diff --git a/changelogs/fragments/383_ec2_snapshot_tags.yml b/changelogs/fragments/383_ec2_snapshot_tags.yml new file mode 100644 index 00000000000..f743e197e71 --- /dev/null +++ b/changelogs/fragments/383_ec2_snapshot_tags.yml @@ -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) diff --git a/plugins/modules/ec2_snapshot.py b/plugins/modules/ec2_snapshot.py index 00e9ac3cdb9..317b1c7c507 100644 --- a/plugins/modules/ec2_snapshot.py +++ b/plugins/modules/ec2_snapshot.py @@ -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 diff --git a/tests/integration/targets/ec2_snapshot/tasks/main.yml b/tests/integration/targets/ec2_snapshot/tasks/main.yml index 94a4c9ab20d..e5d2b412800 100644 --- a/tests/integration/targets/ec2_snapshot/tasks/main.yml +++ b/tests/integration/targets/ec2_snapshot/tasks/main.yml @@ -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: @@ -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 \ No newline at end of file