Last active
August 29, 2015 14:24
-
-
Save juliedavila/93ee794f3d1aca6c33da to your computer and use it in GitHub Desktop.
Tier Route Tables for Ansible 2+
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
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 * | |
from sets import Set | |
class LookupModule(LookupBase): | |
def run(self, terms, variables=None, **kwargs): | |
if isinstance(terms, basestring): | |
terms = [ terms ] | |
ret = [] | |
meta_params = dict(tier=None) | |
try: | |
vpc = boto.vpc.VPCConnection() | |
except Exception, e: | |
raise AnsibleError('Boto authentication issue: %s' % e) | |
for term in terms: | |
eligible_subnets = vpc.get_all_subnets(filters={"tag:tier": term}) | |
for subnet in eligible_subnets: | |
for rt in vpc.get_all_route_tables(filters={'association.subnet_id': subnet.id}): | |
ret.append(rt.id) | |
return Set(ret) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment