diff --git a/changelogs/fragments/ec2_vpc_peer_parameter_checking.yaml b/changelogs/fragments/ec2_vpc_peer_parameter_checking.yaml new file mode 100644 index 00000000000..83ae91795be --- /dev/null +++ b/changelogs/fragments/ec2_vpc_peer_parameter_checking.yaml @@ -0,0 +1,2 @@ +minor_changes: + - Added parameter checking before the module attempts to do an action to give helpful error message diff --git a/changelogs/fragments/ec2_vpc_peering_facts_tags.yml b/changelogs/fragments/ec2_vpc_peering_facts_tags.yml new file mode 100644 index 00000000000..4f6fad5d0b0 --- /dev/null +++ b/changelogs/fragments/ec2_vpc_peering_facts_tags.yml @@ -0,0 +1,2 @@ +minor_changes: + - Changed output of tags dictionary in results to standard Ansible format diff --git a/lib/ansible/modules/cloud/amazon/ec2_vpc_peer.py b/lib/ansible/modules/cloud/amazon/ec2_vpc_peer.py index 58fabe6c3a0..4cf9edbe2f3 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_vpc_peer.py +++ b/lib/ansible/modules/cloud/amazon/ec2_vpc_peer.py @@ -403,11 +403,20 @@ def main(): state=dict(default='present', choices=['present', 'absent', 'accept', 'reject']) ) ) - module = AnsibleModule(argument_spec=argument_spec) + required_if = [ + ('state', 'present', ['vpc_id', 'peer_vpc_id']), + ('state', 'accept', ['peering_id']), + ('state', 'reject', ['peering_id']) + ] + + module = AnsibleModule(argument_spec=argument_spec, required_if=required_if) if not HAS_BOTO3: module.fail_json(msg='json, botocore and boto3 are required.') state = module.params.get('state') + peering_id = module.params.get('peering_id') + vpc_id = module.params.get('vpc_id') + peer_vpc_id = module.params.get('peer_vpc_id') try: region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) client = boto3_conn(module, conn_type='client', resource='ec2', @@ -419,6 +428,9 @@ def main(): (changed, results) = create_peer_connection(client, module) module.exit_json(changed=changed, peering_id=results) elif state == 'absent': + if not peering_id and (not vpc_id or not peer_vpc_id): + module.fail_json(msg='state is absent but one of the following is missing: peering_id or [vpc_id, peer_vpc_id]') + remove_peer_connection(client, module) else: (changed, results) = accept_reject(state, client, module) diff --git a/lib/ansible/modules/cloud/amazon/ec2_vpc_peering_facts.py b/lib/ansible/modules/cloud/amazon/ec2_vpc_peering_facts.py index 983f1afa553..4242ca8378f 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_vpc_peering_facts.py +++ b/lib/ansible/modules/cloud/amazon/ec2_vpc_peering_facts.py @@ -74,7 +74,8 @@ except ImportError: pass # will be picked up by imported HAS_BOTO3 from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.ec2 import (ec2_argument_spec, boto3_conn, get_aws_connection_info, +from ansible.module_utils.ec2 import (boto3_tag_list_to_ansible_dict, + ec2_argument_spec, boto3_conn, get_aws_connection_info, ansible_dict_to_boto3_filter_list, HAS_BOTO3, camel_dict_to_snake_dict) @@ -128,8 +129,13 @@ def main(): except botocore.exceptions.NoCredentialsError as e: module.fail_json(msg=str(e)) + # Turn the boto3 result in to ansible friendly_snaked_names results = [camel_dict_to_snake_dict(peer) for peer in get_vpc_peers(ec2, module)] + # Turn the boto3 result in to ansible friendly tag dictionary + for peer in results: + peer['tags'] = boto3_tag_list_to_ansible_dict(peer.get('tags', [])) + module.exit_json(result=results)