Skip to content

Instantly share code, notes, and snippets.

@arbabnazar
Created January 30, 2016 16:23
Show Gist options
  • Save arbabnazar/06b3722caf9faa21973c to your computer and use it in GitHub Desktop.
Save arbabnazar/06b3722caf9faa21973c to your computer and use it in GitHub Desktop.
'''
USAGE:
- debug:
msg: "{{ lookup('get_sg_id_from_name', (vpc_region, rds_sg_name)) }}"
'''
from ansible.errors import *
from ansible.plugins.lookup import LookupBase
try:
import boto
import boto.ec2
except ImportError:
raise AnsibleError("get_sg_id_from_name lookup cannot be run without boto installed")
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
region = terms[0][0]
sg_name = terms[0][1]
if isinstance(sg_name, basestring):
sg_name = sg_name
ec2_conn = boto.ec2.connect_to_region(region)
sg = ec2_conn.get_all_security_groups(filters={'group_name': sg_name})[0]
return [sg.id]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment