From dbec0b06662e0073dff25b1767e45941363f6077 Mon Sep 17 00:00:00 2001 From: Rachana <1995rachana@gmail.com> Date: Tue, 15 Jun 2021 18:52:06 -0400 Subject: [PATCH] Add fix to change the no_device parameter value to string (#386) Add fix to change the no_device parameter value to string Reviewed-by: https://github.com/apps/ansible-zuul --- .../fragments/386_ec2_ami_no_device.yml | 2 + plugins/modules/ec2_ami.py | 9 ++- .../targets/ec2_ami/tasks/main.yml | 67 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/386_ec2_ami_no_device.yml diff --git a/changelogs/fragments/386_ec2_ami_no_device.yml b/changelogs/fragments/386_ec2_ami_no_device.yml new file mode 100644 index 00000000000..55b43813e7f --- /dev/null +++ b/changelogs/fragments/386_ec2_ami_no_device.yml @@ -0,0 +1,2 @@ +bugfixes: +- ec2_ami - Fix ami issue when creating an ami with no_device parameter (https://github.com/ansible-collections/amazon.aws/pull/386) diff --git a/plugins/modules/ec2_ami.py b/plugins/modules/ec2_ami.py index f246eff0778..36e4aee663a 100644 --- a/plugins/modules/ec2_ami.py +++ b/plugins/modules/ec2_ami.py @@ -457,7 +457,6 @@ def create_image(module, connection): } block_device_mapping = None - # Remove empty values injected by using options if device_mapping: block_device_mapping = [] @@ -474,6 +473,14 @@ def create_image(module, connection): device = rename_item_if_exists(device, 'volume_size', 'VolumeSize', 'Ebs', attribute_type=int) device = rename_item_if_exists(device, 'iops', 'Iops', 'Ebs') device = rename_item_if_exists(device, 'encrypted', 'Encrypted', 'Ebs') + + # The NoDevice parameter in Boto3 is a string. Empty string omits the device from block device mapping + # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_image + if 'NoDevice' in device: + if device['NoDevice'] is True: + device['NoDevice'] = "" + else: + del device['NoDevice'] block_device_mapping.append(device) if block_device_mapping: params['BlockDeviceMappings'] = block_device_mapping diff --git a/tests/integration/targets/ec2_ami/tasks/main.yml b/tests/integration/targets/ec2_ami/tasks/main.yml index 38084ce2525..04c3be171ef 100644 --- a/tests/integration/targets/ec2_ami/tasks/main.yml +++ b/tests/integration/targets/ec2_ami/tasks/main.yml @@ -112,6 +112,59 @@ # ============================================================ + - name: create an image from the instance with attached devices with no_device true + ec2_ami: + name: '{{ ec2_ami_name }}_no_device_true_ami' + instance_id: '{{ setup_instance.instance_ids[0] }}' + device_mapping: + - device_name: /dev/sda1 + volume_size: 10 + delete_on_termination: true + volume_type: gp2 + - device_name: /dev/sdf + no_device: yes + state: present + wait: yes + root_device_name: /dev/xvda + register: result_no_device_true + + - name: set image id fact for deletion later + set_fact: + ec2_ami_no_device_true_image_id: "{{ result_no_device_true.image_id }}" + + - name: assert that image with no_device option yes has been created + assert: + that: + - "result_no_device_true.changed" + - "'/dev/sdf' not in result_no_device_true.block_device_mapping" + + - name: create an image from the instance with attached devices with no_device false + ec2_ami: + name: '{{ ec2_ami_name }}_no_device_false_ami' + instance_id: '{{ setup_instance.instance_ids[0] }}' + device_mapping: + - device_name: /dev/sda1 + volume_size: 10 + delete_on_termination: true + volume_type: gp2 + no_device: no + state: present + wait: yes + root_device_name: /dev/xvda + register: result_no_device_false + + - name: set image id fact for deletion later + set_fact: + ec2_ami_no_device_false_image_id: "{{ result_no_device_false.image_id }}" + + - name: assert that image with no_device option no has been created + assert: + that: + - "result_no_device_false.changed" + - "'/dev/sda1' in result_no_device_false.block_device_mapping" + + # ============================================================ + - name: gather facts about the image created ec2_ami_info: image_ids: '{{ ec2_ami_image_id }}' @@ -437,6 +490,20 @@ wait: yes ignore_errors: yes + - name: delete ami + ec2_ami: + state: absent + image_id: "{{ ec2_ami_no_device_true_image_id }}" + wait: yes + ignore_errors: yes + + - name: delete ami + ec2_ami: + state: absent + image_id: "{{ ec2_ami_no_device_false_image_id }}" + wait: yes + ignore_errors: yes + - name: remove setup snapshot of ec2 instance ec2_snapshot: state: absent