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

ec2_snapshot: Add check_mode support to ec2_snapshot #512

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- ec2_snapshot - add check_mode support
(https://github.com/ansible-collections/amazon.aws/pull/512)
mandar242 marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 6 additions & 6 deletions plugins/modules/ec2_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,39 +71,33 @@
required: false
default: 0
type: int

author: "Will Thames (@willthames)"
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2

'''

EXAMPLES = '''
# Simple snapshot of volume using volume_id
- amazon.aws.ec2_snapshot:
volume_id: vol-abcdef12
description: snapshot of /data from DB123 taken 2013/11/28 12:18:32

mandar242 marked this conversation as resolved.
Show resolved Hide resolved
# Snapshot of volume mounted on device_name attached to instance_id
- amazon.aws.ec2_snapshot:
instance_id: i-12345678
device_name: /dev/sdb1
description: snapshot of /data from DB123 taken 2013/11/28 12:18:32

# Snapshot of volume with tagging
- amazon.aws.ec2_snapshot:
instance_id: i-12345678
device_name: /dev/sdb1
snapshot_tags:
frequency: hourly
source: /data

# Remove a snapshot
- amazon.aws.ec2_snapshot:
snapshot_id: snap-abcd1234
state: absent

# Create a snapshot only if the most recent one is older than 1 hour
- amazon.aws.ec2_snapshot:
volume_id: vol-abcdef12
Expand Down Expand Up @@ -288,6 +282,9 @@ def create_snapshot(module, ec2, description=None, wait=None,
'Tags': ansible_dict_to_boto3_tag_list(_tags),
}]
try:
if module.check_mode:
module.exit_json(changed=True, msg='Would have created a snapshot if not in check mode',
volume_id=volume['VolumeId'], volume_size=volume['Size'])
snapshot = _create_snapshot(ec2, **params)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg="Failed to create snapshot")
Expand Down Expand Up @@ -321,6 +318,8 @@ def create_snapshot(module, ec2, description=None, wait=None,


def delete_snapshot(module, ec2, snapshot_id):
if module.check_mode:
module.exit_json(changed=True, msg='Would have deleted snapshot if not in check mode')
mandar242 marked this conversation as resolved.
Show resolved Hide resolved
try:
ec2.delete_snapshot(aws_retry=True, SnapshotId=snapshot_id)
except is_boto3_error_code('InvalidSnapshot.NotFound'):
Expand Down Expand Up @@ -364,6 +363,7 @@ def create_snapshot_ansible_module():
required_if=required_if,
required_one_of=required_one_of,
required_together=required_together,
supports_check_mode=True
mandar242 marked this conversation as resolved.
Show resolved Hide resolved
)

return module
Expand Down
113 changes: 57 additions & 56 deletions tests/integration/targets/ec2_snapshot/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
# Tests ec2_snapshot_info:
# - Listing snapshots for filter: tag
#
# Possible Bugs:
# - check_mode not supported
#
- name: Integration testing for ec2_snapshot
module_defaults:
group/aws:
Expand All @@ -23,7 +20,6 @@
collections:
- community.aws


block:
- name: Gather availability zones
aws_az_facts:
Expand All @@ -48,7 +44,7 @@
- untagged_snapshot.snapshots| length == 1
- untagged_snapshot.snapshots[0].volume_id == volume_detached.volume_id

- name: Setup an instance for testing
- name: Setup an instance for testing, make sure volumes are attached before next task
ec2_instance:
name: '{{ resource_prefix }}'
instance_type: t2.nano
Expand All @@ -60,23 +56,27 @@
volume_size: 8
delete_on_termination: true
register: instance
retries: 5
delay: 60
until: "instance.instances[0].block_device_mappings | length > 0 and instance.instances[0].block_device_mappings[-1].ebs.status =='attached'"
mandar242 marked this conversation as resolved.
Show resolved Hide resolved

- set_fact:
volume_id: '{{ instance.instances[0].block_device_mappings[0].ebs.volume_id }}'
instance_id: '{{ instance.instances[0].instance_id }}'
device_name: '{{ instance.instances[0].block_device_mappings[0].device_name }}'

# JR: Check mode not supported
# - name: Take snapshot (check mode)
# ec2_snapshot:
# instance_id: '{{ instance_id }}'
# check_mode: true
# snapshot_tags:
# Test: '{{ resource_prefix }}'
# register: result
# - assert:
# that:
# - result is changed
- name: Take snapshot (check mode)
ec2_snapshot:
instance_id: '{{ instance_id }}'
device_name: '{{ device_name }}'
snapshot_tags:
Test: '{{ resource_prefix }}'
check_mode: true
register: result

- assert:
that:
- result is changed

- name: Take snapshot of volume
ec2_snapshot:
Expand Down Expand Up @@ -105,7 +105,7 @@
filters:
"tag:Name": '{{ resource_prefix }}'
register: info_check
check_mode: yes
check_mode: true

- assert:
that:
Expand All @@ -116,18 +116,17 @@
- info_check.snapshots[0].volume_size == result.volume_size
- info_check.snapshots[0].tags == result.tags

# JR: Check mode not supported
# - name: Take snapshot if most recent >1hr (False) (check mode)
# ec2_snapshot:
# volume_id: '{{ volume_id }}'
# snapshot_tags:
# Name: '{{ resource_prefix }}'
# last_snapshot_min_age: 60
# check_mode: true
# register: result
# - assert:
# that:
# - result is not changed
- name: Take snapshot if most recent >1hr (False) (check mode)
ec2_snapshot:
volume_id: '{{ volume_id }}'
snapshot_tags:
Name: '{{ resource_prefix }}'
last_snapshot_min_age: 60
check_mode: true
register: result
- assert:
that:
- result is not changed

- name: Take snapshot if most recent >1hr (False)
ec2_snapshot:
Expand All @@ -150,18 +149,17 @@
pause:
minutes: 1

# JR: Check mode not supported
# - name: Take snapshot if most recent >1min (True) (check mode)
# ec2_snapshot:
# volume_id: '{{ volume_id }}'
# snapshot_tags:
# Name: '{{ resource_prefix }}'
# last_snapshot_min_age: 1
# check_mode: true
# register: result
# - assert:
# that:
# - result is changed
- name: Take snapshot if most recent >1min (True) (check mode)
ec2_snapshot:
volume_id: '{{ volume_id }}'
snapshot_tags:
Name: '{{ resource_prefix }}'
last_snapshot_min_age: 1
check_mode: true
register: result
- assert:
that:
- result is changed

- name: Take snapshot if most recent >1min (True)
ec2_snapshot:
Expand All @@ -181,16 +179,16 @@
- info_result.snapshots| length == 2
- result.snapshot_id in ( info_result.snapshots | map(attribute='snapshot_id') | list )

# JR: Check mode not supported
# - name: Take snapshot with a tag (check mode)
# ec2_snapshot:
# volume_id: '{{ volume_id }}'
# snapshot_tags:
# MyTag: '{{ resource_prefix }}'
# register: result
# - assert:
# that:
# - result is changed
- name: Take snapshot with a tag (check mode)
ec2_snapshot:
volume_id: '{{ volume_id }}'
snapshot_tags:
MyTag: '{{ resource_prefix }}'
check_mode: true
register: result
- assert:
that:
- result is changed

- name: Take snapshot and tag it
ec2_snapshot:
Expand Down Expand Up @@ -222,11 +220,8 @@
"tag:Name": '{{ resource_prefix }}'
register: info_result

- assert:
that:
- info_result.snapshots| length == 3

- ec2_snapshot:
mandar242 marked this conversation as resolved.
Show resolved Hide resolved
- name: Generate extra snapshots
ec2_snapshot:
volume_id: '{{ volume_id }}'
snapshot_tags:
ResourcePrefix: '{{ resource_prefix }}'
Expand All @@ -242,6 +237,12 @@
pause:
minutes: 2

- name: Get info about all snapshots for this test
ec2_snapshot_info:
filters:
"tag:Name": '{{ resource_prefix }}'
register: info_result

# check that snapshot_ids and max_results are mutually exclusive
- name: Check that max_results and snapshot_ids are mutually exclusive
ec2_snapshot_info:
Expand Down