--- - name: check if name is a symlink stat: path: "{{ image_filename }}" register: symlnk_check - name: bypass image naming logic if image name is provided (backwards-compatibility) set_fact: amphora_image: "{{ amphora_image_name }}" when: - amphora_image_name is defined - not ((amphora_image_name | length) < 1) - name: set the actual glance image name if it is a symlink set_fact: amphora_image: "{{ (symlnk_check.stat.lnk_target | basename | splitext)[0] }}" when: - amphora_image is undefined - symlnk_check is defined and (symlnk_check.stat.islnk | bool) - name: set the actual glance image name if it is not a symlink set_fact: amphora_image: "{{ (image_file_result.stat.path | basename | splitext)[0] }}" when: - amphora_image is undefined - (image_file_result.stat.exists | bool) and (not (symlnk_check.stat.islnk | bool)) - name: Capture the file's checksum set_fact: image_checksum: "{{ image_file_result.stat.checksum }}" when: - image_file_result.stat.exists | bool - name: Check the amphora image file format command: | qemu-img info --output=json "{{ image_filename }}" register: image_file_format - name: Set the image format set_fact: img_format: "{{ (image_file_format.stdout | from_json).format }}" - name: Convert image if indicated when: - amp_to_raw | bool - img_format != "raw" block: - name: create temporary directory tempfile: state: directory register: amp_tmp_dir - name: set RAW file name set_fact: raw_filename: "{{ amp_tmp_dir.path }}/{{ image_filename|splitext|first|basename }}.img" - name: convert image from qcow2 to raw shell: | qemu-img convert -f qcow2 -O raw {{ image_filename }} {{ raw_filename }} - name: get the checksum for the converted file stat: path: "{{ raw_filename }}" get_checksum: true register: raw_file_result - name: update image_checksum with checksum of the converted file set_fact: image_checksum: "{{ raw_file_result.stat.checksum }}" - name: setting amphora format to raw set_fact: img_format: raw - name: gather facts about the service project shell: | openstack project show "{{ auth_project_name }}" -c id -f value register: project_id_result - name: check there's an image in glance already shell: | openstack image list --property owner={{ project_id_result.stdout }} --private --name {{ amphora_image }} -c ID -f value environment: OS_USERNAME: "{{ auth_username }}" OS_PASSWORD: "{{ auth_password }}" OS_PROJECT_NAME: "{{ auth_project_name }}" register: glance_id_result failed_when: false - name: set image id fact set_fact: image_id: "{{ glance_id_result.stdout }}" when: - glance_id_result.rc is defined - glance_id_result.rc == 0 - glance_id_result.stdout != "" - name: get checksum if there's an image in glance already shell: | openstack image show {{ glance_id_result.stdout }} -c properties -f json environment: OS_USERNAME: "{{ auth_username }}" OS_PASSWORD: "{{ auth_password }}" OS_PROJECT_NAME: "{{ auth_project_name }}" when: - image_id is defined register: glance_results failed_when: false - name: set current_checksum fact from glance if image already exists there set_fact: current_image_facts: "{{ glance_results.stdout | from_json }}" when: - glance_results.rc is defined - glance_results.rc == 0 - name: store the current checksum if available when: - current_image_facts.properties.image_checksum is defined set_fact: current_checksum: "{{ current_image_facts.properties.image_checksum }}" - name: calculate the image checksum if it is missing when: - image_id is defined - current_checksum is not defined block: - name: create temporary directory tempfile: state: directory register: amp_tmp_dir - name: download the current amphora image command: | openstack image save --file "{{ amp_tmp_dir.path }}/{{ image_id }}.tmp" {{ image_id }} environment: OS_USERNAME: "{{ auth_username }}" OS_PASSWORD: "{{ auth_password }}" OS_PROJECT_NAME: "{{ auth_project_name }}" - name: calculate the missing checksum stat: path: "{{ amp_tmp_dir.path }}/{{ image_id }}.tmp" get_checksum: true register: tmp_file_result - name: update current checksum fact set_fact: current_checksum: "{{ tmp_file_result.stat.checksum }}" - name: store the property on the image so it is there next time command: | openstack image set --property image_checksum={{ current_checksum }} {{ image_id }} environment: OS_USERNAME: "{{ auth_username }}" OS_PASSWORD: "{{ auth_password }}" OS_PROJECT_NAME: "{{ auth_project_name }}" - name: remove the temporary copy of the current amphora image file: path: "{{ amp_tmp_dir.path }}/{{ image_id }}.tmp" state: absent - name: determine if the image needs to be replaced set_fact: replace_image: "{{ current_checksum != image_checksum }}" when: - current_checksum is defined - image_checksum is defined - name: move existing image if the names match and the checksums are not the same shell: | ts=`openstack image show {{ image_id }} -f value -c created_at` ts=${ts//:/} ts=${ts//-/} openstack image set {{ image_id }} --name "{{ amphora_image }}_$ts" environment: OS_USERNAME: "{{ auth_username }}" OS_PASSWORD: "{{ auth_password }}" OS_PROJECT_NAME: "{{ auth_project_name }}" when: - replace_image is defined and replace_image | bool - name: decide whether to upload new image set_fact: upload_image: true when: - (current_checksum is not defined) or (replace_image is defined and replace_image | bool) - name: upload image to glance shell: | openstack image create --disk-format {{ img_format|default('qcow2') }} \ --container-format bare --tag {{ amp_image_tag }} \ --file {{ raw_filename|default(image_filename) }} \ --property hw_architecture={{ amp_hw_arch }} \ --property image_checksum={{ image_checksum }} \ --private {{ amphora_image }} environment: OS_USERNAME: "{{ auth_username }}" OS_PASSWORD: "{{ auth_password }}" OS_PROJECT_NAME: "{{ auth_project_name }}" register: image_result changed_when: (image_result.stdout | length) < 1 when: - image_file_result.stat.exists | bool - upload_image is defined - name: delete converted raw image when: - amp_tmp_dir.path is defined file: path: "{{ amp_tmp_dir.path }}" state: absent