Last active
August 29, 2015 14:24
-
-
Save juliedavila/f10a762e8e2531c1f874 to your computer and use it in GitHub Desktop.
Get all the subnets in a particular vpc tier
This file contains hidden or 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_subnets', 'vpc_id=vpc-7fb0841a tier=dmz1') }}" | |
from __future__ import (absolute_import, division, print_function) | |
__metaclass__ = type | |
import boto | |
import boto.vpc | |
import boto.ec2 | |
from ansible.plugins.lookup import LookupBase | |
from ansible.errors import AnsibleError | |
from ansible.module_utils.ec2 import * | |
class LookupModule(LookupBase): | |
def run(self, terms, variables=None, **kwargs): | |
if isinstance(terms, basestring): | |
terms = [ terms ] | |
ret = [] | |
meta_params = dict(vpc_id=None, tier=None) | |
try: | |
vpc = boto.vpc.VPCConnection() | |
except Exception, e: | |
raise AnsibleError('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: | |
raise AnsibleError(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