Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix to change the no_device parameter value to string #386

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
srirachanaachyuthuni marked this conversation as resolved.
Show resolved Hide resolved
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"
srirachanaachyuthuni marked this conversation as resolved.
Show resolved Hide resolved
- "'/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