├── .gitignore ├── LICENSE ├── README.md ├── ansible_version.py ├── aws_account_id.py ├── aws_check_s3_key.py ├── aws_ec2_allocation_id_from_eip.py ├── aws_ec2_instance_id_from_name.py ├── aws_ec2_instance_private_ip_from_name.py ├── aws_ec2_instance_public_ip_from_name.py ├── aws_ec2_instance_status_from_id.py ├── aws_ec2_instance_status_from_name.py ├── aws_ec2_nat_gateway_id_from_allocation_id.py ├── aws_ec2_nat_gateway_id_from_eip.py ├── aws_ec2_target_group_arn_from_name.py ├── aws_rds_endpoint_name_from_instance_name.py ├── aws_rds_endpoint_port_from_instance_name.py ├── aws_secgroup_ids_from_names.py ├── aws_subnet_ids_from_names.py ├── aws_vpc_id_from_name.py ├── my_public_ip.py └── v1 ├── __init__.py ├── aws_account_id.py ├── aws_ec2_instance_id_from_name.py ├── aws_ec2_instance_public_ip_from_name.py ├── aws_ec2_instance_status_from_name.py ├── aws_rds_endpoint_name_from_instance_name.py ├── aws_rds_endpoint_port_from_instance_name.py ├── aws_secgroup_ids_from_names.py ├── aws_subnet_ids_from_names.py └── aws_vpc_id_from_name.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | *.py[cod] 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jon Hadfield 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ansible-lookups 2 | ======== 3 | 4 | Additional ansible lookup plugins used to query external variables. 5 | 6 | v1 = compatible with legacy 1.x versions of Ansible 7 | 8 | 9 | Installation 10 | ------------ 11 | Place the files in the lookup_plugins directory specified in ansible.cfg. 12 | 13 | 14 | Usage 15 | ------------ 16 | Each file contains a description and example usage. 17 | -------------------------------------------------------------------------------- /ansible_version.py: -------------------------------------------------------------------------------- 1 | # (c) 2017, Jon Hadfield 2 | """ 3 | Description: This lookup retrieves the ansible version. Note: Only works on Ansible 2.2 and above. 4 | 5 | Example Usage: 6 | {{ lookup('ansible_version') }} 7 | """ 8 | from __future__ import (absolute_import, division, print_function) 9 | __metaclass__ = type 10 | 11 | from ansible.errors import AnsibleError 12 | from ansible.plugins.lookup import LookupBase 13 | from ansible.release import __version__ as ansible_version 14 | 15 | 16 | class LookupModule(LookupBase): 17 | def run(self, terms, variables=None, **kwargs): 18 | return [ansible_version] 19 | -------------------------------------------------------------------------------- /aws_account_id.py: -------------------------------------------------------------------------------- 1 | # (c) 2017, Jon Hadfield 2 | """ 3 | Description: This lookup retrieves the numerical id of the AWS account that 4 | matches the discovered credentials. You might want to use this to check that 5 | Ansible is going to manage the correct account before you run any tasks. 6 | 7 | Example Usage: 8 | {{ lookup('aws_account_id') }} 9 | """ 10 | from __future__ import (absolute_import, division, print_function) 11 | __metaclass__ = type 12 | 13 | from ansible.errors import * 14 | from ansible.plugins.lookup import LookupBase 15 | 16 | try: 17 | import boto3 18 | except ImportError: 19 | raise AnsibleError("aws_account_id lookup cannot be run without boto3 installed") 20 | try: 21 | import simplejson as json 22 | except ImportError: 23 | import json 24 | from six.moves import urllib 25 | 26 | 27 | class LookupModule(LookupBase): 28 | def run(self, terms, variables=None, **kwargs): 29 | session = boto3.Session() 30 | # Try getting from current user 31 | try: 32 | iam_client = session.client('iam') 33 | return [iam_client.get_user().arn.split(':')[4]] 34 | except: 35 | # User doesn't exist 36 | pass 37 | # Try getting from sts 38 | try: 39 | sts_client = session.client('sts') 40 | return [sts_client.get_caller_identity()['Account']] 41 | except: 42 | pass 43 | # Try getting from instance role 44 | try: 45 | response = urllib.request.urlopen('http://169.254.169.254/latest/meta-data/iam/info/') 46 | response_content = response.read() 47 | json_output = json.loads(response_content) 48 | arn = json_output.get('InstanceProfileArn') 49 | return [arn.split(':')[4]] 50 | except: 51 | pass -------------------------------------------------------------------------------- /aws_check_s3_key.py: -------------------------------------------------------------------------------- 1 | # (c) 2016, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a path and checks for the presence of a specific key in S3. 4 | This may be used for failing early if an object isn't present or as part of a conditional upload task. 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_instance_id_from_name', ('eu-west-1', 'mybucket/hello.txt') }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | __metaclass__ = type 11 | 12 | from ansible.errors import * 13 | from ansible.plugins.lookup import LookupBase 14 | 15 | try: 16 | import boto3 17 | except ImportError: 18 | raise AnsibleError("aws_check_s3_key lookup cannot be run without boto installed") 19 | try: 20 | import simplejson as json 21 | except ImportError: 22 | import json 23 | 24 | class LookupModule(LookupBase): 25 | def run(self, terms, variables=None, **kwargs): 26 | region = terms[0][0] 27 | split_path = terms[0][1].split('/') 28 | bucket = split_path[0] 29 | key = '/'.join(split_path[1:]) 30 | s3 = boto3.client('s3') 31 | try: 32 | s3.head_object(Bucket=bucket, Key=key) 33 | return 'True' 34 | except: 35 | pass 36 | -------------------------------------------------------------------------------- /aws_ec2_allocation_id_from_eip.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and an elastic ip address 4 | and returns the allocation id. 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_allocation_id_from_eip', ('eu-west-1', '54.54.54.54') }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | __metaclass__ = type 11 | 12 | import os 13 | import codecs 14 | 15 | from ansible.errors import * 16 | from ansible.plugins.lookup import LookupBase 17 | 18 | try: 19 | import boto3 20 | import botocore 21 | except ImportError: 22 | raise AnsibleError("aws_ec2_allocation_id_from_eip lookup cannot be run without boto installed") 23 | 24 | class LookupModule(LookupBase): 25 | 26 | def run(self, terms, variables=None, **kwargs): 27 | region = terms[0][0] 28 | public_ip = terms[0][1] 29 | session=boto3.session.Session(region_name=region) 30 | 31 | try: 32 | ec2_client=session.connect('ec2') 33 | except botocore.exception.NoRegionError: 34 | raise AnsibleError("AWS region not specified") 35 | 36 | ip_filter=[{'Name': 'public-ip', 'Values': [public_ip]}] 37 | 38 | result=ec2_client.describe_addresses(Filders=ip_filter) 39 | 40 | if result and result.get('Addresses'): 41 | return [result.get('Addresses')[0].get('AllocationId').encode('utf-8')] 42 | return None 43 | -------------------------------------------------------------------------------- /aws_ec2_instance_id_from_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a 'Name' tag 4 | value of an ec2 instance and returns the instance id. 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_instance_id_from_name', ('eu-west-1', 'server1') }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | __metaclass__ = type 11 | 12 | from ansible.errors import * 13 | from ansible.plugins.lookup import LookupBase 14 | 15 | try: 16 | import boto3 17 | import botocore 18 | except ImportError: 19 | raise AnsibleError("aws_ec2_instance_id_from_name lookup cannot be run without boto installed") 20 | 21 | class LookupModule(LookupBase): 22 | 23 | def run(self, terms, variables=None, **kwargs): 24 | region = terms[0][0] 25 | instance_name = terms[0][1] 26 | 27 | session=boto3.session.Session(region_name=region) 28 | try: 29 | ec2_client=session.client('ec2') 30 | except botocore.exception.NoRegionError: 31 | raise AnsibleError("AWS region not specified") 32 | 33 | instance_filter=[{'Name':'tag:Name', 'Values': [instance_name]}, 34 | {'Name': 'instance-state-name', 'Values': ['running']}] 35 | 36 | result=ec2_client.describe_instances(Filters=instance_filter) 37 | 38 | if result and result.get('Reservations'): 39 | return [result.get('Reservations')[0].get('Instances')[0].get('InstanceId').encode('utf-8')] 40 | return None 41 | -------------------------------------------------------------------------------- /aws_ec2_instance_private_ip_from_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a 'Name' tag value 4 | of and ec2 instance and returns the current private IP address. 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_instance_private_ip_from_name', ('eu-west-1', 'server1') }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | __metaclass__ = type 11 | 12 | import os 13 | import codecs 14 | 15 | from ansible.errors import * 16 | from ansible.plugins.lookup import LookupBase 17 | 18 | try: 19 | import boto3 20 | import botocore 21 | except ImportError: 22 | raise AnsibleError("aws_ec2_instance_private_ip_from_name lookup cannot be run without boto3 installed") 23 | 24 | class LookupModule(LookupBase): 25 | 26 | def run(self, terms, variables=None, **kwargs): 27 | region = terms[0][0] 28 | instance_name = terms[0][1] 29 | 30 | session=boto3.session.Session(region_name=region) 31 | 32 | try: 33 | ec2_client=session.client('ec2') 34 | except botocore.exceptions.NoRegionError: 35 | raise AnsibleError("AWS region must be specified") 36 | 37 | instance_filter=[{'Name': 'tag:Name', 'Values': [instance_name]}, 38 | {'Name': 'instance-state-name', 'Values': ['running']}] 39 | 40 | result=ec2_client.describe_instances(Filters=instance_filter) 41 | 42 | reservations=result.get('Reservations') 43 | 44 | if reservations and reservations[0].get('Instances'): 45 | return [reservations[0].get('Instances')[0].get('PrivateIpAddress').encode('utf-8')] 46 | return None 47 | -------------------------------------------------------------------------------- /aws_ec2_instance_public_ip_from_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a 'Name' tag value 4 | of and ec2 instance and returns the current public IP address. 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_instance_public_ip_from_name', ('eu-west-1', 'server1') }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | 11 | __metaclass__ = type 12 | 13 | import os 14 | import codecs 15 | 16 | from ansible.errors import * 17 | from ansible.plugins.lookup import LookupBase 18 | 19 | try: 20 | import boto3 21 | import botocore 22 | except ImportError: 23 | raise AnsibleError("aws_ec2_instance_public_ip_from_name lookup cannot be run without boto3 installed") 24 | 25 | 26 | class LookupModule(LookupBase): 27 | 28 | def run(self, terms, variables=None, **kwargs): 29 | region = terms[0][0] 30 | instance_name = terms[0][1] 31 | 32 | session = boto3.session.Session(region_name=region) 33 | 34 | try: 35 | ec2_client = session.client('ec2') 36 | except botocore.exceptions.NoRegionError: 37 | raise AnsibleError("AWS region must be specified") 38 | 39 | instance_filter = [{'Name': 'tag:Name', 'Values': [instance_name]}, 40 | {'Name': 'instance-state-name', 'Values': ['running']}] 41 | 42 | result = ec2_client.describe_instances(Filters=instance_filter) 43 | 44 | result = result.get('Reservations') 45 | 46 | if result and result[0].get('Instances')[0].get('PublicIpAddress'): 47 | return [result[0].get('Instances')[0].get('PublicIpAddress').encode('utf-8')] 48 | return None 49 | -------------------------------------------------------------------------------- /aws_ec2_instance_status_from_id.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a 'Name' tag value 4 | of and ec2 instance and returns the current state. 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_instance_status_from_name', ('eu-west-1', 'server1') }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | __metaclass__ = type 11 | 12 | # import os 13 | # import codecs 14 | 15 | from ansible.errors import * 16 | from ansible.plugins.lookup import LookupBase 17 | 18 | try: 19 | # import boto 20 | # import boto.ec2 21 | 22 | import boto3 23 | import botocore 24 | except ImportError: 25 | raise AnsibleError("aws_ec2_instance_status_from_name lookup cannot be run without boto installed") 26 | 27 | class LookupModule(LookupBase): 28 | 29 | def run(self, terms, variables=None, **kwargs): 30 | region=terms[0][0] 31 | instance_id=terms[0][1] 32 | 33 | session=boto3.session.Session(region_name=region) 34 | 35 | try: 36 | ec2_client=session.client('ec2') 37 | except botocore.exceptions.NoRegionError: 38 | raise AnsibleError("AWS region not specified") 39 | 40 | instance_filter=[{'Name': 'instance-id', 'Values': [instance_id]}] 41 | 42 | result=ec2_client.describe_instance_status(InstanceIds=[instance_id]) 43 | 44 | if result and result.get('InstanceStatuses'): 45 | return [result.get('InstanceStatuses')[0].get('InstanceState').get('Name').encode('utf-8')] 46 | return None -------------------------------------------------------------------------------- /aws_ec2_instance_status_from_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a 'Name' tag value 4 | of and ec2 instance and returns the current state. 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_instance_status_from_name', ('eu-west-1', 'server1') }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | 11 | __metaclass__ = type 12 | 13 | import os 14 | import codecs 15 | 16 | from ansible.errors import * 17 | from ansible.plugins.lookup import LookupBase 18 | 19 | try: 20 | import boto3 21 | import botocore 22 | 23 | # import boto 24 | # import boto.ec2 25 | except ImportError: 26 | raise AnsibleError("aws_ec2_instance_status_from_name lookup cannot be run without boto3 installed") 27 | 28 | 29 | class LookupModule(LookupBase): 30 | 31 | def run(self, terms, variables=None, **kwargs): 32 | region = terms[0][0] 33 | instance_name = terms[0][1] 34 | 35 | session = boto3.session.Session(region_name=region) 36 | try: 37 | ec2_client = session.client('ec2') 38 | except botocore.exceptions.NoRegionError: 39 | raise AnsibleError("AWS region must be specified") 40 | 41 | instance_filter = [{'Name': 'tag:Name', 'Values': [instance_name]}] 42 | 43 | result = ec2_client.describe_instances(Filters=instance_filter) 44 | reservations = result.get('Reservations') 45 | 46 | if reservations and reservations[0].get('Instances')[0].get('State').get('Name'): 47 | return [reservations[0].get('Instances')[0].get('State').get('Name').encode('utf-8')] 48 | return None 49 | 50 | -------------------------------------------------------------------------------- /aws_ec2_nat_gateway_id_from_allocation_id.py: -------------------------------------------------------------------------------- 1 | # (c) 2016, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and an allocation id 4 | and returns the associated NAT gateway id 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_nat_gateway_id_from_allocation_id', ('eu-west-1', 'eipalloc-68b33333') }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | __metaclass__ = type 11 | 12 | import os 13 | import codecs 14 | 15 | from ansible.errors import * 16 | from ansible.plugins.lookup import LookupBase 17 | 18 | try: 19 | import botocore 20 | import boto3 21 | except ImportError: 22 | raise AnsibleError("aws_ec2_nat_gateway_id_from_allocation_id lookup cannot be run without botocore and boto3 installed") 23 | 24 | class LookupModule(LookupBase): 25 | 26 | def run(self, terms, variables=None, **kwargs): 27 | region = terms[0][0] 28 | allocation_id = terms[0][1] 29 | session = boto3.session.Session(region_name=region) 30 | try: 31 | ec2_client = session.client('ec2') 32 | except botocore.exceptions.NoRegionError: 33 | raise AnsibleError("AWS region not specified.") 34 | filter = [{'Name': 'state','Values': ['available']}] 35 | result = ec2_client.describe_nat_gateways(Filter=filter) 36 | nat_gateways = result.get('NatGateways') 37 | if nat_gateways: 38 | for nat_gateway in nat_gateways: 39 | nat_gateway_addresses = nat_gateway.get('NatGatewayAddresses') 40 | if nat_gateway_addresses: 41 | for nat_gateway_address in nat_gateway_addresses: 42 | if nat_gateway_address.get('AllocationId') == allocation_id: 43 | return [nat_gateway.get('NatGatewayId').encode('utf-8')] 44 | -------------------------------------------------------------------------------- /aws_ec2_nat_gateway_id_from_eip.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and an elastic ip address 4 | and returns the associated NAT gateway id 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_nat_gateway_id_from_eip', ('eu-west-1', '54.54.54.54') }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | __metaclass__ = type 11 | 12 | import os 13 | import codecs 14 | 15 | from ansible.errors import * 16 | from ansible.plugins.lookup import LookupBase 17 | 18 | try: 19 | import botocore 20 | import boto3 21 | except ImportError: 22 | raise AnsibleError("aws_ec2_allocation_id_from_eip lookup cannot be run without botocore and boto3 installed") 23 | 24 | class LookupModule(LookupBase): 25 | 26 | def run(self, terms, variables=None, **kwargs): 27 | region = terms[0][0] 28 | eip = terms[0][1] 29 | session = boto3.session.Session(region_name=region) 30 | try: 31 | ec2_client = session.client('ec2') 32 | except botocore.exceptions.NoRegionError: 33 | raise AnsibleError("AWS region not specified.") 34 | filter = [{'Name': 'state','Values': ['available']}] 35 | result = ec2_client.describe_nat_gateways(Filter=filter) 36 | nat_gateways = result.get('NatGateways') 37 | if nat_gateways: 38 | for nat_gateway in nat_gateways: 39 | nat_gateway_addresses = nat_gateway.get('NatGatewayAddresses') 40 | if nat_gateway_addresses: 41 | for nat_gateway_address in nat_gateway_addresses: 42 | if nat_gateway_address.get('PublicIp') == eip: 43 | return [nat_gateway.get('NatGatewayId').encode('utf-8')] 44 | -------------------------------------------------------------------------------- /aws_ec2_target_group_arn_from_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2016, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a target group name 4 | and returns the target group arn 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_target_group_arn_from_name', ('eu-west-1', 'tgname') }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | __metaclass__ = type 11 | 12 | from ansible.errors import * 13 | from ansible.plugins.lookup import LookupBase 14 | 15 | try: 16 | import botocore 17 | import boto3 18 | except ImportError: 19 | raise AnsibleError("aws_ec2_target_group_arn_from_name lookup cannot be run without botocore and boto3 installed") 20 | 21 | class LookupModule(LookupBase): 22 | def run(self, terms, variables=None, **kwargs): 23 | region = terms[0][0] 24 | tg_name = terms[0][1] 25 | session = boto3.session.Session(region_name=region) 26 | try: 27 | elbv2_client = session.client('elbv2') 28 | except botocore.exceptions.NoRegionError: 29 | raise AnsibleError("AWS region not specified.") 30 | result = elbv2_client.describe_target_groups(Names=[tg_name]) 31 | target_groups = result.get('TargetGroups') 32 | if target_groups: 33 | return [target_groups[0]['TargetGroupArn'].encode('utf-8')] 34 | -------------------------------------------------------------------------------- /aws_rds_endpoint_name_from_instance_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and an RDS instance 4 | name and returns the endpoint name. 5 | 6 | Example Usage: 7 | {{ lookup('aws_rds_endpoint_name_from_instance_name', ('eu-west-1', 'mydb')) }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | __metaclass__ = type 11 | 12 | from ansible.errors import * 13 | from ansible.plugins.lookup import LookupBase 14 | 15 | try: 16 | import boto3 17 | import botocore 18 | except ImportError: 19 | raise AnsibleError("aws_rds_endpoint_name_from_instance_name lookup cannot be run without boto installed") 20 | 21 | class LookupModule(LookupBase): 22 | def run(self, terms, variables=None, **kwargs): 23 | region = terms[0][0] 24 | instance_name = terms[0][1] 25 | 26 | session=boto3.session.Session(region_name=region) 27 | 28 | try: 29 | rds_client=session.client('rds') 30 | except botocore.exceptions.NoRegionError: 31 | raise AnsibleError("AWS region not specified.") 32 | 33 | result=rds_client.describe_db_instances(DBInstanceIdentifier=instance_name) 34 | 35 | if result and result.get('DBInstances'): 36 | return [result.get('DBInstances')[0].get('Endpoint').get('HostedZoneId').encode('utf-8')] 37 | return None 38 | -------------------------------------------------------------------------------- /aws_rds_endpoint_port_from_instance_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and an RDS instance 4 | name and returns the endpoint port. 5 | 6 | Example Usage: 7 | {{ lookup('aws_rds_endpoint_port_from_instance_name', ('eu-west-1', 'mydb')) }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | __metaclass__ = type 11 | 12 | from ansible.errors import * 13 | from ansible.plugins.lookup import LookupBase 14 | 15 | try: 16 | import boto3 17 | import botocore 18 | except ImportError: 19 | raise AnsibleError("aws_rds_endpoint_port_from_instance_name lookup cannot be run without boto installed") 20 | 21 | 22 | class LookupModule(LookupBase): 23 | def run(self, terms, variables=None, **kwargs): 24 | region = terms[0][0] 25 | instance_name = terms[0][1] 26 | 27 | session=boto3.session.Session(region_name=region) 28 | 29 | try: 30 | rds_client=session.client('rds') 31 | except botocore.exceptions.NoRegionError: 32 | raise AnsibleError("AWS region not specified.") 33 | 34 | result=rds_client.describe_db_instances(DBInstanceIdentifier=instance_name) 35 | 36 | if result and result.get('DBInstances'): 37 | return [result.get('DBInstances')[0].get('Endpoint').get('Port').encode('utf-8')] 38 | return None 39 | -------------------------------------------------------------------------------- /aws_secgroup_ids_from_names.py: -------------------------------------------------------------------------------- 1 | # (c) 2017, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a list of one or more 4 | security Group Names and returns a list of matching security Group IDs. 5 | 6 | Example Usage: 7 | {{ lookup('aws_secgroup_ids_from_names', ('eu-west-1', ['nginx_group', 'mysql_group'])) }} 8 | """ 9 | 10 | from __future__ import (absolute_import, division, print_function) 11 | 12 | __metaclass__ = type 13 | 14 | from ansible.errors import * 15 | from ansible.plugins.lookup import LookupBase 16 | 17 | try: 18 | import boto3 19 | import botocore 20 | except ImportError: 21 | raise AnsibleError("aws_secgroup_ids_from_names lookup cannot be run without boto installed") 22 | 23 | 24 | class LookupModule(LookupBase): 25 | def run(self, terms, variables=None, **kwargs): 26 | region = terms[0][0] 27 | group_names = terms[0][1] 28 | if isinstance(group_names, basestring): 29 | group_names = [group_names] 30 | session = boto3.session.Session(region_name=region) 31 | try: 32 | ec2_client = session.client('ec2') 33 | except botocore.exceptions.NoRegionError: 34 | raise AnsibleError("AWS region not specified.") 35 | # for group_name in group_names: 36 | secgroup_filter = [{'Name': 'group-name', 'Values': group_names}] 37 | result = ec2_client.describe_security_groups(Filters=secgroup_filter) 38 | groups = result.get('SecurityGroups') 39 | group_ids = [] 40 | if groups: 41 | for group in groups: 42 | group_ids.append(group.get('GroupId').encode('utf-8')) 43 | return group_ids 44 | -------------------------------------------------------------------------------- /aws_subnet_ids_from_names.py: -------------------------------------------------------------------------------- 1 | # (c) 2017, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a list of one or more 4 | subnet names and returns a list of matching subnet ids. 5 | 6 | Example Usage: 7 | {{ lookup('aws_subnet_ids_from_names', ('eu-west-1', ['subnet1', 'subnet2'])) }} 8 | """ 9 | from __future__ import (absolute_import, division, print_function) 10 | 11 | __metaclass__ = type 12 | 13 | from ansible.errors import * 14 | from ansible.plugins.lookup import LookupBase 15 | 16 | try: 17 | import boto3 18 | import botocore 19 | except ImportError: 20 | raise AnsibleError("aws_subnet_ids_from_names lookup cannot be run without boto installed") 21 | 22 | 23 | class LookupModule(LookupBase): 24 | def run(self, terms, variables=None, **kwargs): 25 | if isinstance(terms, basestring): 26 | terms = [terms] 27 | subnet_ids = [] 28 | region = terms[0][0] 29 | subnet_names = terms[0][1] 30 | session = boto3.session.Session(region_name=region) 31 | try: 32 | ec2_client = session.client('ec2') 33 | except botocore.exceptions.NoRegionError: 34 | raise AnsibleError("AWS region not specified.") 35 | subnet_filter = [{'Name': 'tag:Name', 'Values': subnet_names}] 36 | result = ec2_client.describe_subnets(Filters=subnet_filter) 37 | subnets = result.get('Subnets') 38 | if subnets: 39 | for subnet in subnets: 40 | subnet_ids.append(subnet.get('SubnetId').encode('utf-8')) 41 | return subnet_ids 42 | -------------------------------------------------------------------------------- /aws_vpc_id_from_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2017, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a vpc 4 | name and returns a matching VPC ID. 5 | 6 | Example Usage: 7 | {{ lookup('aws_vpc_id_from_name', ('eu-west-1', 'vpc1')) }} 8 | """ 9 | 10 | from __future__ import (absolute_import, division, print_function) 11 | 12 | __metaclass__ = type 13 | 14 | from ansible.errors import * 15 | from ansible.plugins.lookup import LookupBase 16 | 17 | try: 18 | import boto3 19 | import botocore 20 | except ImportError: 21 | raise AnsibleError("aws_vpc_id_from_name lookup cannot be run without boto installed") 22 | 23 | 24 | class LookupModule(LookupBase): 25 | def run(self, terms, variables=None, **kwargs): 26 | if isinstance(terms, basestring): 27 | terms = [terms] 28 | vpc_ids = [] 29 | region = terms[0][0] 30 | vpc_names = terms[0][1] 31 | session = boto3.session.Session(region_name=region) 32 | try: 33 | ec2_client = session.client('ec2') 34 | except botocore.exceptions.NoRegionError: 35 | raise AnsibleError("AWS region not specified.") 36 | vpc_filter = [{'Name': 'tag:Name', 'Values': [vpc_names]}] 37 | result = ec2_client.describe_vpcs(Filters=vpc_filter) 38 | vpcs = result.get('Vpcs') 39 | if vpcs: 40 | vpc_ids.append(vpcs[0].get('VpcId').encode('utf-8')) 41 | return vpc_ids 42 | -------------------------------------------------------------------------------- /my_public_ip.py: -------------------------------------------------------------------------------- 1 | # (c) 2016, Jon Hadfield 2 | """ 3 | Description: This lookup returns your public IP by querying multiple services. 4 | 5 | Example Usage: 6 | {{ lookup('my_public_ip')) }} 7 | """ 8 | from __future__ import (absolute_import, division, print_function) 9 | __metaclass__ = type 10 | 11 | from ansible.errors import * 12 | from ansible.plugins.lookup import LookupBase 13 | import socket 14 | from json import load 15 | from urllib2 import urlopen 16 | 17 | def is_valid_ip(value): 18 | try: 19 | socket.inet_aton(value) 20 | return True 21 | except socket.error: 22 | return False 23 | 24 | class LookupModule(LookupBase): 25 | def run(self, terms, **kwargs): 26 | try: 27 | value = load(urlopen('http://httpbin.org/ip'))['origin'] 28 | if is_valid_ip(value): 29 | return [value] 30 | except: 31 | pass 32 | try: 33 | value = load(urlopen('https://api.ipify.org/?format=json'))['ip'] 34 | if is_valid_ip(value): 35 | return [value] 36 | except: 37 | pass 38 | try: 39 | value = load(urlopen('http://jsonip.com'))['ip'] 40 | if is_valid_ip(value): 41 | return [value] 42 | except: 43 | pass 44 | 45 | -------------------------------------------------------------------------------- /v1/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonhadfield/ansible-lookups/19eb7308f0c22ed4e5fd8e5492876ce4d331533f/v1/__init__.py -------------------------------------------------------------------------------- /v1/aws_account_id.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup retrieves the numerical id of the AWS account that 4 | matches the discovered credentials. You might want to use this to check that 5 | Ansible is going to manage the correct account before you run any tasks. 6 | 7 | Example Usage: 8 | {{ lookup('aws_account_id') }} 9 | """ 10 | from ansible import errors 11 | try: 12 | import boto 13 | except ImportError: 14 | raise AnsibleError("aws_account_id lookup cannot be run without boto installed") 15 | try: 16 | import simplejson as json 17 | except ImportError: 18 | import json 19 | from six.moves import urllib 20 | 21 | 22 | class LookupModule(object): 23 | def __init__(self, basedir=None, **kwargs): 24 | self.basedir = basedir 25 | 26 | def run(self, **kwargs): 27 | try: 28 | iam_conn = boto.connect_iam() 29 | return [iam_conn.get_user().arn.split(':')[4]] 30 | except: 31 | response = urllib.request.urlopen('http://169.254.169.254/latest/meta-data/iam/info/') 32 | response_content = response.read() 33 | json_output = json.loads(response_content) 34 | arn = json_output.get('InstanceProfileArn') 35 | return [arn.split(':')[4]] 36 | -------------------------------------------------------------------------------- /v1/aws_ec2_instance_id_from_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a 'Name' tag 4 | value of and ec2 instance and returns the instance id. 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_instance_id_from_name', ('eu-west-1', 'server1') }} 8 | """ 9 | from ansible import errors 10 | try: 11 | import boto.ec2 12 | except ImportError: 13 | raise AnsibleError("aws_ec2_instance_id_from_name lookup cannot be run without boto installed") 14 | 15 | class LookupModule(object): 16 | def __init__(self, basedir=None, **kwargs): 17 | self.basedir = basedir 18 | 19 | def run(self, terms, variables=None, **kwargs): 20 | region = terms[0] 21 | instance_name = terms[1] 22 | conn = boto.ec2.connect_to_region(region) 23 | filters = {'tag:Name': instance_name} 24 | ec2_instance = conn.get_only_instances(filters=filters) 25 | if ec2_instance and ec2_instance[0].id: 26 | return [ec2_instance[0].id.encode('utf-8')] 27 | return None 28 | -------------------------------------------------------------------------------- /v1/aws_ec2_instance_public_ip_from_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a 'Name' tag value 4 | of and ec2 instance and returns the current public IP address. 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_instance_public_ip_from_name', ('eu-west-1', 'server1') }} 8 | """ 9 | from ansible import errors 10 | try: 11 | import boto.ec2 12 | except ImportError: 13 | raise AnsibleError("aws_ec2_instance_public_ip_from_name lookup cannot be run without boto installed") 14 | 15 | class LookupModule(object): 16 | def __init__(self, basedir=None, **kwargs): 17 | self.basedir = basedir 18 | 19 | def run(self, terms, variables=None, **kwargs): 20 | region = terms[0] 21 | instance_name = terms[1] 22 | conn = boto.ec2.connect_to_region(region) 23 | filters = {'tag:Name': instance_name} 24 | ec2_instance = conn.get_only_instances(filters=filters) 25 | if ec2_instance and ec2_instance[0].ip_address: 26 | return [ec2_instance[0].ip_address.encode('utf-8')] 27 | return None 28 | -------------------------------------------------------------------------------- /v1/aws_ec2_instance_status_from_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a 'Name' tag value 4 | of and ec2 instance and returns the current state. 5 | 6 | Example Usage: 7 | {{ lookup('aws_ec2_instance_status_from_name', ('eu-west-1', 'server1') }} 8 | """ 9 | from ansible import errors 10 | try: 11 | import boto.ec2 12 | except ImportError: 13 | raise AnsibleError("aws_ec2_instance_status_from_name lookup cannot be run without boto installed") 14 | 15 | class LookupModule(object): 16 | def __init__(self, basedir=None, **kwargs): 17 | self.basedir = basedir 18 | 19 | def run(self, terms, variables=None, **kwargs): 20 | region = terms[0] 21 | instance_name = terms[1] 22 | conn = boto.ec2.connect_to_region(region) 23 | filters = {'tag:Name': instance_name} 24 | ec2_instance = conn.get_only_instances(filters=filters) 25 | if ec2_instance and ec2_instance[0].state: 26 | return [ec2_instance[0].state.encode('utf-8')] 27 | return None 28 | -------------------------------------------------------------------------------- /v1/aws_rds_endpoint_name_from_instance_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and an RDS instance 4 | name and returns the endpoint name. 5 | 6 | Example Usage: 7 | {{ lookup('aws_rds_endpoint_name_from_instance_name', ('eu-west-1', 'mydb')) }} 8 | """ 9 | from ansible import errors 10 | try: 11 | import boto.rds 12 | except ImportError: 13 | raise AnsibleError("aws_rds_endpoint_name_from_instance_name lookup cannot be run without boto installed") 14 | 15 | 16 | class LookupModule(object): 17 | def __init__(self, basedir=None, **kwargs): 18 | self.basedir = basedir 19 | 20 | def run(self, terms, variables=None, **kwargs): 21 | region = terms[0] 22 | instance_name = terms[1] 23 | db_conn = boto.rds.connect_to_region(region) 24 | db = db_conn.get_all_dbinstances(instance_name) 25 | if db and db[0]: 26 | return [db[0].endpoint[0]] 27 | return None 28 | -------------------------------------------------------------------------------- /v1/aws_rds_endpoint_port_from_instance_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and an RDS instance 4 | name and returns the endpoint port. 5 | 6 | Example Usage: 7 | {{ lookup('aws_rds_endpoint_port_from_instance_name', ('eu-west-1', 'mydb')) }} 8 | """ 9 | from ansible import errors 10 | try: 11 | import boto.rds 12 | except ImportError: 13 | raise AnsibleError("aws_rds_endpoint_port_from_instance_name lookup cannot be run without boto installed") 14 | 15 | 16 | class LookupModule(object): 17 | def __init__(self, basedir=None, **kwargs): 18 | self.basedir = basedir 19 | 20 | def run(self, terms, variables=None, **kwargs): 21 | region = terms[0] 22 | instance_name = terms[1] 23 | db_conn = boto.rds.connect_to_region(region) 24 | db = db_conn.get_all_dbinstances(instance_name) 25 | if db and db[0]: 26 | return [str(db[0].endpoint[1])] 27 | return None 28 | -------------------------------------------------------------------------------- /v1/aws_secgroup_ids_from_names.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a list of one or more 4 | security Group Names and returns a list of matching security Group IDs. 5 | 6 | Example Usage: 7 | {{ lookup('aws_secgroup_ids_from_names', ('eu-west-1', ['nginx_group', 'mysql_group'])) }} 8 | """ 9 | from ansible import errors 10 | try: 11 | import boto.ec2 12 | except ImportError: 13 | raise errors.AnsibleError("aws_secgroup_ids_from_names lookup cannot be run without boto installed") 14 | 15 | class LookupModule(object): 16 | def __init__(self, basedir=None, **kwargs): 17 | self.basedir = basedir 18 | 19 | def run(self, terms, variables=None, **kwargs): 20 | if isinstance(terms, basestring): 21 | terms = [terms] 22 | region = terms[0] 23 | group_names = terms[1] 24 | conn = boto.ec2.connect_to_region(region) 25 | filters = {'group_name': group_names} 26 | sg = conn.get_all_security_groups(filters=filters) 27 | sg_list = [x.id.encode('utf-8') for x in sg] 28 | return sg_list 29 | -------------------------------------------------------------------------------- /v1/aws_subnet_ids_from_names.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a list of one or more 4 | subnet names and returns a list of matching subnet ids. 5 | 6 | Example Usage: 7 | {{ lookup('aws_subnet_ids_from_names', ('eu-west-1', ['subnet1', 'subnet2'])) }} 8 | """ 9 | from ansible import errors 10 | try: 11 | import boto.vpc 12 | except ImportError: 13 | raise errors.AnsibleError("aws_subnet_ids_from_names lookup cannot be run without boto installed") 14 | 15 | class LookupModule(object): 16 | def __init__(self, basedir=None, **kwargs): 17 | self.basedir = basedir 18 | 19 | def run(self, terms, inject=None, **kwargs): 20 | if isinstance(terms, basestring): 21 | terms = [terms] 22 | subnet_ids = [] 23 | region = terms[0] 24 | subnet_names = terms[1] 25 | vpc_conn = boto.vpc.connect_to_region(region) 26 | filters = {'tag:Name': terms[1]} 27 | subnets = vpc_conn.get_all_subnets(filters=filters) 28 | subnet_ids = [x.id.encode('utf-8') for x in subnets] 29 | return subnet_ids 30 | 31 | -------------------------------------------------------------------------------- /v1/aws_vpc_id_from_name.py: -------------------------------------------------------------------------------- 1 | # (c) 2015, Jon Hadfield 2 | """ 3 | Description: This lookup takes an AWS region and a vpc 4 | name and returns a matching VPC ID. 5 | 6 | Example Usage: 7 | {{ lookup('aws_vpc_id_from_name', ('eu-west-1', 'vpc1')) }} 8 | """ 9 | from ansible import errors 10 | try: 11 | import boto.vpc 12 | except ImportError: 13 | raise AnsibleError("aws_vpc_id_from_name lookup cannot be run without boto installed") 14 | 15 | 16 | class LookupModule(object): 17 | def __init__(self, basedir=None, **kwargs): 18 | self.basedir = basedir 19 | 20 | def run(self, terms, variables=None, **kwargs): 21 | region = terms[0] 22 | vpc_name = terms[1] 23 | vpc_conn = boto.vpc.connect_to_region(region) 24 | filters = {'tag:Name': vpc_name} 25 | vpc = vpc_conn.get_all_vpcs(filters=filters) 26 | if vpc and vpc[0]: 27 | return [vpc[0].id.encode('utf-8')] 28 | return None 29 | --------------------------------------------------------------------------------