--- - name: Fetch all images one_image_info: register: all_images - name: Print all images debug: var: all_images - name: Gather facts about an image using a name one_image_info: name: '{{ one_image_name }}' register: image_with_name - name: Verify image's facts assert: that: - not image_with_name is changed - image_with_name.images|length == 1 - image_with_name.images[0].name == '{{ one_image_name }}' - not image_with_name.images[0].used|bool - image_with_name.images[0].running_vms == 0 - name: Gather facts about the image using ID one_image_info: id: '{{ image_with_name.images[0].id }}' register: image_with_ids - name: Verify image's facts assert: that: - not image_with_ids is changed - image_with_ids.images|length == 1 - image_with_ids.images[0].name == '{{ one_image_name }}' - not image_with_ids.images[0].used|bool - image_with_ids.images[0].running_vms == 0 - name: Try to gather facts about an image using a name and ids one_image_info: name: '{{ one_image_name }}' id: '{{ image_with_name.images[0].id }}' register: image_name_ids failed_when: not image_name_ids is failed - name: Try to fetch non-existent image by name one_image_info: name: non-existent-vm-{{ ansible_date_time.iso8601_basic_short }} register: image_missing failed_when: not image_missing is failed - name: Try to gather facts about non-existent images by regex one_image_info: name: ~non-existent-vm-{{ ansible_date_time.iso8601_basic_short }}-* register: images_with_regex - name: Verify that images list is empty assert: that: - not images_with_regex is changed - images_with_regex.images|length == 0 - name: Try to fetch non-existent image by id one_image_info: id: -999 register: image_missing failed_when: not image_missing is failed - block: - name: Clone the image first time one_image: name: '{{ one_image_name }}' state: cloned new_name: '{{ one_image_name }}-clone-1' - name: Clone the image second time one_image: name: '{{ one_image_name }}' state: cloned new_name: '{{ one_image_name }}-clone-2' - name: Fetch all images whose name matches regex one_image_info: name: '~{{ one_image_name }}-clone-[12]$' register: cloned_instances - name: Check there are 2 matched instances assert: that: - not cloned_instances is changed - cloned_instances.images|length == 2 - cloned_instances.images[0].name == "{{ one_image_name }}-clone-1" - cloned_instances.images[1].name == "{{ one_image_name }}-clone-2" msg: "There should be 2 cloned instances" - name: Gather facts about all images using IDs one_image_info: ids: - '{{ cloned_instances.images[0].id }}' - '{{ cloned_instances.images[1].id }}' register: cloned_instances_with_ids - name: Check there are 2 matched instances assert: that: - not cloned_instances_with_ids is changed - cloned_instances_with_ids.images|length == 2 - cloned_instances_with_ids.images[0].name == "{{ one_image_name }}-clone-1" - cloned_instances_with_ids.images[1].name == "{{ one_image_name }}-clone-2" msg: "There should be 2 cloned instances" - name: Rename the second image one_image: id: '{{ cloned_instances_with_ids.images[1].id }}' state: renamed new_name: '{{ one_image_name }}-CLONE-2' - name: Fetch all images whose name matches regex one_image_info: name: '~{{ one_image_name }}-clone-[12]$' register: cloned_instances - name: Check there is only 1 matched instance assert: that: - not cloned_instances is changed - cloned_instances.images|length == 1 - cloned_instances.images[0].name == "{{ one_image_name }}-clone-1" msg: "There should be 1 cloned instance" - name: Fetch all images whose name matches regex ignoring cases one_image_info: name: '~*{{ one_image_name }}-clone-[12]$' register: cloned_instances_case_insensitive - name: Check there are 2 matched instances assert: that: - not cloned_instances_case_insensitive is changed - cloned_instances_case_insensitive.images|length == 2 - cloned_instances_case_insensitive.images[0].name == "{{ one_image_name }}-clone-1" - cloned_instances_case_insensitive.images[1].name == "{{ one_image_name }}-CLONE-2" msg: "There should be 2 cloned instances" - name: Delete cloned instances one_image: id: '{{ item.id }}' state: absent with_items: '{{ cloned_instances.images }}' always: - name: Delete the first cloned image one_image: name: '{{ one_image_name }}-clone-1' state: absent - name: Delete the second cloned image one_image: name: '{{ one_image_name }}-clone-2' state: absent - name: Delete the second cloned image one_image: name: '{{ one_image_name }}-CLONE-2' state: absent