From 54f9f06edca3c6c97f2f31d0d5c563ad1cfa6ff9 Mon Sep 17 00:00:00 2001 From: Julien Vey Date: Wed, 21 Feb 2018 20:48:51 +0100 Subject: [PATCH] [cloud] ec2_group: fix check mode when using tags (#36503) Fixes #36412 --- lib/ansible/modules/cloud/amazon/ec2_group.py | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_group.py b/lib/ansible/modules/cloud/amazon/ec2_group.py index 8f61c316e1..6ac6f3aabb 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_group.py +++ b/lib/ansible/modules/cloud/amazon/ec2_group.py @@ -789,22 +789,24 @@ def main(): changed = True - if tags is not None: + if tags is not None and group is not None: current_tags = boto3_tag_list_to_ansible_dict(group.get('Tags', [])) tags_need_modify, tags_to_delete = compare_aws_tags(current_tags, tags, purge_tags) if tags_to_delete: - try: - client.delete_tags(Resources=[group['GroupId']], Tags=[{'Key': tag} for tag in tags_to_delete]) - except botocore.exceptions.ClientError as e: - module.fail_json(msg=e.message, exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) + if not module.check_mode: + try: + client.delete_tags(Resources=[group['GroupId']], Tags=[{'Key': tag} for tag in tags_to_delete]) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=e.message, exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) changed = True # Add/update tags if tags_need_modify: - try: - client.create_tags(Resources=[group['GroupId']], Tags=ansible_dict_to_boto3_tag_list(tags_need_modify)) - except botocore.exceptions.ClientError as e: - module.fail_json(msg=e.message, exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) + if not module.check_mode: + try: + client.create_tags(Resources=[group['GroupId']], Tags=ansible_dict_to_boto3_tag_list(tags_need_modify)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=e.message, exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) changed = True else: