Skip to content

Commit

Permalink
Add fix to change the no_device parameter value to string (#386)
Browse files Browse the repository at this point in the history
Add fix to change the no_device parameter value to string 

Reviewed-by: https://github.com/apps/ansible-zuul
  • Loading branch information
srirachanaachyuthuni authored Jun 15, 2021
1 parent 714b3a8 commit dbec0b0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/386_ec2_ami_no_device.yml
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 8 additions & 1 deletion plugins/modules/ec2_ami.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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
Expand Down
67 changes: 67 additions & 0 deletions tests/integration/targets/ec2_ami/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dbec0b0

Please sign in to comment.