def find_vpc(module, vpc_conn, vpc_id=None, cidr=None):
 if ((vpc_id == None) and (cidr == None)):
  module.fail_json(msg='You must specify either a vpc_id or a cidr block + list of unique tags, aborting')
 found_vpcs = []
 resource_tags = module.params.get('resource_tags')
 if (vpc_id is not None):
  found_vpcs = vpc_conn.get_all_vpcs(None, {'vpc-id': vpc_id, 'state': 'available'})
 else:
  previous_vpcs = vpc_conn.get_all_vpcs(None, {'cidr': cidr, 'state': 'available'})
  for vpc in previous_vpcs:
   vpc_tags = dict(((t.name, t.value) for t in vpc_conn.get_all_tags(filters={'resource-id': vpc.id})))
   if (resource_tags and set(resource_tags.items()).issubset(set(vpc_tags.items()))):
    found_vpcs.append(vpc)
 found_vpc = None
 if (len(found_vpcs) == 1):
  found_vpc = found_vpcs[0]
 if (len(found_vpcs) > 1):
  module.fail_json(msg='Found more than one vpc based on the supplied criteria, aborting')
 return found_vpc

def find_vpc ( module , vpc_conn , vpc_id = None , cidr = None ) : if ( ( vpc_id == None ) and ( cidr == None ) ) : module . fail_json ( msg = 'You must specify either a vpc_id or a cidr block + list of unique tags, aborting' ) found_vpcs = [ ] resource_tags = module . params . get ( 'resource_tags' ) if ( vpc_id is not None ) : found_vpcs = vpc_conn . get_all_vpcs ( None , { 'vpc-id' : vpc_id , 'state' : 'available' } ) else : previous_vpcs = vpc_conn . get_all_vpcs ( None , { 'cidr' : cidr , 'state' : 'available' } ) for vpc in previous_vpcs : vpc_tags = dict ( ( ( t . name , t . value ) for t in vpc_conn . get_all_tags ( filters = { 'resource-id' : vpc . id } ) ) ) if ( resource_tags and set ( resource_tags . items ( ) ) . issubset ( set ( vpc_tags . items ( ) ) ) ) : found_vpcs . append ( vpc ) found_vpc = None if ( len ( found_vpcs ) == 1 ) : found_vpc = found_vpcs [ 0 ] if ( len ( found_vpcs ) > 1 ) : module . fail_json ( msg = 'Found more than one vpc based on the supplied criteria, aborting' ) return found_vpc
finds a vpc that matches a specific id or cidr + tags module : ansiblemodule object vpc_conn: authenticated vpcconnection connection object returns: a vpc object that matches either an id or cidr and one or more tag values .