Skip to content

Commit

Permalink
Add volume tags key with empty dict and test (#383)
Browse files Browse the repository at this point in the history
Add volume tags key with empty dict and test

Reviewed-by: https://github.com/apps/ansible-zuul
  • Loading branch information
srirachanaachyuthuni authored Jun 5, 2021
1 parent 1e18f10 commit 0cdc47b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/383_ec2_snapshot_tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ec2_snapshot - Fix snapshot issue when capturing a snapshot of a volume without tags (https://github.com/ansible-collections/amazon.aws/pull/383)
3 changes: 2 additions & 1 deletion plugins/modules/ec2_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ def create_snapshot(module, ec2, description=None, wait=None,
volume_id = volume['VolumeId']
else:
volume = get_volume_by_id(module, ec2, volume_id)

if 'Tags' not in volume:
volume['Tags'] = {}
if last_snapshot_min_age > 0:
current_snapshots = get_snapshots_by_volume(module, ec2, volume_id)
last_snapshot_min_age = last_snapshot_min_age * 60 # Convert to seconds
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/targets/ec2_snapshot/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@


block:
- name: Gather availability zones
aws_az_facts:
register: azs

# Create a new volume in detached mode without tags
- name: Create a detached volume without tags
ec2_vol:
volume_size: 1
zone: '{{ azs.availability_zones[0].zone_name }}'
register: volume_detached

# Capture snapshot of this detached volume and assert the results
- name: Create a snapshot of detached volume without tags and store results
ec2_snapshot:
volume_id: '{{ volume_detached.volume_id }}'
register: untagged_snapshot

- assert:
that:
- untagged_snapshot is changed
- untagged_snapshot.snapshots| length == 1
- untagged_snapshot.snapshots[0].volume_id == volume_detached.volume_id

- ec2_ami_info:
owners: amazon
filters:
Expand Down Expand Up @@ -340,3 +363,15 @@
id: '{{ volume_id }}'
state: absent
ignore_errors: true

- name: Delete detached and untagged volume
ec2_vol:
id: '{{ volume_detached.volume_id}}'
state: absent
ignore_errors: true

- name: Delete untagged snapshot
ec2_snapshot:
state: absent
snapshot_id: '{{ untagged_snapshot.snapshot_id }}'
ignore_errors: true

0 comments on commit 0cdc47b

Please sign in to comment.