From f8caf2d40c5d0d51b77b9852d8a510891c890f0a Mon Sep 17 00:00:00 2001 From: abikouo Date: Thu, 6 Jul 2023 18:01:45 +0200 Subject: [PATCH] fix: test_customized_ami when trying to create existing AMI with custom_ami_recreate_if_exists set to ``false`` --- roles/customized_ami/tasks/create.yaml | 22 ++++++++++++++++++++-- roles/customized_ami/tasks/delete.yaml | 3 --- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/roles/customized_ami/tasks/create.yaml b/roles/customized_ami/tasks/create.yaml index 687c014d..7f8fb842 100644 --- a/roles/customized_ami/tasks/create.yaml +++ b/roles/customized_ami/tasks/create.yaml @@ -1,8 +1,27 @@ --- +- name: List existing AMI by name + amazon.aws.ec2_ami_info: + filters: + name: "{{ custom_ami_name }}" + register: customized_ami__existing_amis + - name: Delete existing images - ansible.builtin.include_tasks: delete.yaml + amazon.aws.ec2_ami: + name: "{{ item.name }}" + image_id: "{{ item.image_id }}" + wait: true + state: absent + with_items: "{{ customized_ami__existing_amis.images }}" + when: custom_ami_recreate_if_exists | bool + +- name: List existing AMI by name + amazon.aws.ec2_ami_info: + filters: + name: "{{ custom_ami_name }}" + register: customized_ami__existing_amis - name: Create custom AMI + when: customized_ami__existing_amis.images | length == 0 block: - name: Get Source AMI identifier ansible.builtin.include_tasks: read_source_ami.yaml @@ -22,4 +41,3 @@ msg: "Existing AMI found with name: '{{ custom_ami_name }}'" when: - customized_ami__existing_amis.images | length > 0 - - not (custom_ami_recreate_if_exists | bool) diff --git a/roles/customized_ami/tasks/delete.yaml b/roles/customized_ami/tasks/delete.yaml index 4440ed93..c6a6cefb 100644 --- a/roles/customized_ami/tasks/delete.yaml +++ b/roles/customized_ami/tasks/delete.yaml @@ -12,6 +12,3 @@ wait: true state: absent with_items: "{{ customized_ami__existing_amis.images }}" - when: - - customized_ami__existing_amis.images | length > 0 - - (custom_ami_operation == 'create') | ternary(custom_ami_recreate_if_exists, 'true') | bool