---
# Copyright 2023, Cleura AB.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Fetch the list of already existing images
openstack.cloud.image_info:
cloud: "{{ openstack_resources_cloud_name }}"
interface: "{{ openstack_resources_interface }}"
when:
- openstack_resources_image['images'] | selectattr('checksum', 'defined')
- not (openstack_resources_image['image_force_upload'] | default(false))
register: existing_images
- name: Upload images
ansible.builtin.include_tasks:
file: image_upload.yml
loop: "{{ images_for_upload | batch(openstack_resources_image['image_upload_batch'] | default(3)) }}"
loop_control:
loop_var: images
label: "{{ images_for_upload | map(attribute='name') | to_json }}"
vars:
existing_images_checksums: "{{ existing_images['images'] | default([]) | map(attribute='checksum') }}"
images_for_upload: >-
{{
(openstack_resources_image['image_force_upload'] | default(false)) | ternary(
openstack_resources_image['images'],
openstack_resources_image['images'] | selectattr('checksum', 'defined') | rejectattr('checksum', 'in', existing_images_checksums) +
openstack_resources_image['images'] | rejectattr('checksum', 'defined')
)
}}
- name: Retrieve fresh details about controlled OpenStack images
openstack.cloud.image_info:
cloud: "{{ openstack_resources_cloud_name }}"
interface: "{{ openstack_resources_interface }}"
filters:
"name": "{{ image['name'] }}"
register: available_images
loop: "{{ openstack_resources_image['images'] | rejectattr('checksum', 'undefined') }}"
loop_control:
loop_var: image
label: "name={{ image['name'] }}"
- name: Define images to rotate
ansible.builtin.set_fact:
images_to_rotate: |-
{% set image_rotate = {} %}
{% for image_result in available_images['results'] %}
{% set _ = image_rotate.update({
image_result['image']['name']: image_result['images'] | rejectattr('checksum', 'eq', image_result['image']['checksum'])
})
%}
{% endfor %}
{{ image_rotate }}
- name: Looping over images to rotate
ansible.builtin.include_tasks:
file: image_rotate.yml
loop: "{{ available_images['results'] | map(attribute='image') }}"
loop_control:
loop_var: image
label: "{{ image['name'] }}"
when:
- images_to_rotate[image['name']] | length > 0