Add role to discover Centos latest images

With this patch, we are adding a new role discover-latest-image to
discover the latest Centos Stream image, We are limiting the scope
for C9 for now, We can extend the role for Centos 8 image discovery
in future if there is a need.

Also, we are updating baremetal-prep-virthost.yml playbook to use
this new role for c9 case and in combination with patch[1] - we will
get rid of undercloud baseos hardcoding and use the latest discovered
image.

[1] https://review.opendev.org/c/openstack/tripleo-quickstart/+/838990/

Change-Id: I3a97edc63e96725a1690335f557508d56ed10258
This commit is contained in:
Sandeep Yadav 2022-05-04 13:51:52 +05:30
parent 8de2c51407
commit 4a83b740d7
4 changed files with 102 additions and 0 deletions

View File

@ -60,6 +60,25 @@
- ansible_distribution == 'RedHat'
- ansible_distribution_major_version|int >= 8
- name: Find latest Centos image and set facts for undercloud base image.
hosts: virthost
gather_facts: true
tasks:
- block:
- name: Discover latest CentOS qcow2 image
include_role:
name: discover-latest-image
- name: set_fact for undercloud base image
set_fact:
baseos_undercloud_image_url: "{{ discovered_image_url }}"
baseos_image: "{{ ansible_distribution | lower }}"
baseos_image_type: qcow2
baseos_md5sum: "{{ discovered_md5sum }} {{ discovered_image_name }}"
cacheable: true
when:
- ansible_distribution == 'CentOS'
- ansible_distribution_major_version is version(9, '>=')
- name: Setup undercloud and baremetal vms and networks in libvirt
hosts: virthost
gather_facts: true

View File

@ -0,0 +1,35 @@
Role Name
=========
An Ansible role to discover latest Centos image.
Requirements
------------
This role is currently written for Centos-9 and can be extended to Centos-8 on need basis.
Role Variables
--------------
* base_url: <https://cloud.centos.org/centos/{{ ansible_distribution_major_version }}-stream/x86_64/images/> Base Url from where we can pull the Centos Image.
* qcow_prefix: <CentOS-Stream-GenericCloud-> Qcow2 image prefix on base_url which will be used as a filter to find latest Centos Image.
Example Playbook
----------------
1. Sample playbook to call the role
- name: Discover latest CentOS qcow2 image
include_role:
name: discover-latest-image
2. Sample config to use the facts from discover-latest-image
- name: set_fact for undercloud base image
set_fact:
baseos_undercloud_image_url: "{{ discovered_image_url }}"
baseos_image: "{{ ansible_distribution | lower }}"
baseos_image_type: qcow2
baseos_md5sum: "{{ discovered_md5sum }} {{ discovered_image_name }}"
cacheable: true

View File

@ -0,0 +1,3 @@
---
base_url: https://cloud.centos.org/centos/{{ ansible_distribution_major_version }}-stream/x86_64/images/
qcow_prefix: CentOS-Stream-GenericCloud-

View File

@ -0,0 +1,45 @@
---
- name: Create a temporary file
tempfile:
state: file
register: tempfile
- name: Remove the file at temporary file path.
file:
path: "{{ tempfile.path }}"
state: absent
- name: Fetch publication page
get_url:
url: "{{ base_url }}?C=M;O=A"
dest: "{{ tempfile.path }}"
- name: Find qcow2 url
shell: |
sed -n "/qcow2/ s/.*\({{ qcow_prefix }}.*.qcow2\)<\/a>.*/\1/p" {{ tempfile.path }} | tail -1
register: get_qcow_image_name
- name: Remove the file at temporary file path.
file:
path: "{{ tempfile.path }}"
state: absent
- name: Fetch md5sum file
get_url:
url: "{{ base_url }}/{{ get_qcow_image_name.stdout }}.MD5SUM"
dest: "{{ tempfile.path }}"
- name: Find MD5 value
command: "awk -F ' = ' '/MD5/ {print $2}' {{ tempfile.path }}"
register: get_md5_value
- name: Remove the file at temporary file path.
file:
path: "{{ tempfile.path }}"
state: absent
- name: "Set expected facts Image: {{ base_url }}{{ get_qcow_image_name.stdout }} Md5sum: {{ get_md5_value.stdout }} "
set_fact:
discovered_image_name: "{{ get_qcow_image_name.stdout }}"
discovered_image_url: "{{ base_url }}{{ get_qcow_image_name.stdout }}"
discovered_md5sum: "{{ get_md5_value.stdout }}"