From 87bd169ca908e2c78a4dbedf7471035b5017d1f0 Mon Sep 17 00:00:00 2001 From: Ryan Brown Date: Mon, 2 Apr 2018 17:13:44 -0400 Subject: [PATCH] Allow subnets with names formatted like `subnet-1234` (#37740) * Allow subnets with names formatted like `subnet-1234` * Deduplicate IDs, in case a subnet is specified both by name and ID --- lib/ansible/modules/cloud/amazon/ec2_vpc_nacl.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_vpc_nacl.py b/lib/ansible/modules/cloud/amazon/ec2_vpc_nacl.py index d8fad3ec27..c151e2f28e 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_vpc_nacl.py +++ b/lib/ansible/modules/cloud/amazon/ec2_vpc_nacl.py @@ -149,6 +149,7 @@ try: except ImportError: HAS_BOTO3 = False +import traceback from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info @@ -530,19 +531,22 @@ def subnets_to_associate(nacl, client, module): params = list(module.params.get('subnets')) if not params: return [] - if params[0].startswith("subnet-"): + all_found = [] + if any(x.startswith("subnet-") for x in params): try: subnets = client.describe_subnets(Filters=[ {'Name': 'subnet-id', 'Values': params}]) + all_found.extend(subnets.get('Subnets', [])) except botocore.exceptions.ClientError as e: - module.fail_json(msg=str(e)) - else: + module.fail_json(msg=str(e), exception=traceback.format_exc()) + if len(params) != len(all_found): try: subnets = client.describe_subnets(Filters=[ {'Name': 'tag:Name', 'Values': params}]) + all_found.extend(subnets.get('Subnets', [])) except botocore.exceptions.ClientError as e: - module.fail_json(msg=str(e)) - return [s['SubnetId'] for s in subnets['Subnets'] if s['SubnetId']] + module.fail_json(msg=str(e), exception=traceback.format_exc()) + return list(set(s['SubnetId'] for s in all_found if s.get('SubnetId'))) def main():