Last active
August 29, 2015 14:24
-
-
Save juliedavila/32be943c867ef292cfbe to your computer and use it in GitHub Desktop.
Subnets in a tier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Usage: lookup('tier_subnetsV1', 'vpc_id=vpc-2344 tier=dmz1') | |
import boto | |
import boto.vpc | |
import boto.ec2 | |
from ansible import utils | |
class LookupModule(object): | |
def __init__(self, basedir=None, **kwargs): | |
self.basedir = basedir | |
def run(self, terms, inject=None, **kwargs): | |
terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) | |
if isinstance(terms, basestring): | |
terms = [ terms ] | |
ret = [] | |
meta_params = dict(vpc_id=None, tier=None) | |
try: | |
vpc = boto.vpc.VPCConnection() | |
except Exception, e: | |
utils.warnings('Boto authentication issue: %s' % e) | |
for term in terms: | |
params = term.split() | |
try: | |
for param in params: | |
key, value = param.split('=') | |
assert(key in meta_params) | |
if key == 'vpc_id': | |
meta_params['vpc_id'] = value | |
if key == 'tier': | |
meta_params['tier'] = value | |
except (ValueError, AssertionError) as e: | |
utils.warnings(e) | |
vpc_id=meta_params['vpc_id'] | |
tier=meta_params['tier'] | |
filter_params={ | |
'vpc_id': vpc_id, | |
'tag:tier': tier | |
} | |
subnets_in_vpc = vpc.get_all_subnets(filters=filter_params) | |
for subnet in subnets_in_vpc: | |
ret.append(subnet.id) | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment