Refactor ec2_remote_facts to use filters

pull/18777/head
whiter 9 years ago committed by Matt Clay
parent 6d166d75bc
commit d8dc9485f4

@ -16,137 +16,149 @@
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: ec2_remote_facts module: ec2_remote_facts
short_description: ask EC2 for information about other instances. short_description: Gather facts about ec2 instances in AWS
description: description:
- Only supports search for hostname by tags currently. Looking to add more later. - Gather facts about ec2 instances in AWS
version_added: "2.0" version_added: "2.0"
options: options:
key: filters:
description: description:
- instance tag key in EC2 - A dict of filters to apply. Each dict item consists of a filter key and a filter value. See U(http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstances.html) for possible filters.
required: false
default: Name
value:
description:
- instance tag value in EC2
required: false required: false
default: null default: null
lookup:
description:
- What type of lookup to use when searching EC2 instance info.
required: false
default: tags
region:
description:
- EC2 region that it should look for tags in
required: false
default: All Regions
ignore_state:
description:
- instance state that should be ignored such as terminated.
required: false
default: terminated
author: author:
- "Michael Schuett (@michaeljs1990)" - "Michael Schuett (@michaeljs1990)"
extends_documentation_fragment: aws extends_documentation_fragment:
- aws
- ec2
''' '''
EXAMPLES = ''' EXAMPLES = '''
# Note: These examples do not set authentication details, see the AWS Guide for details. # Note: These examples do not set authentication details, see the AWS Guide for details.
# Basic provisioning example # Gather facts about all ec2 instances
- ec2_remote_facts: - ec2_remote_facts:
key: mykey
value: myvalue # Gather facts about all running ec2 instances with a tag of Name:Example
register: servers - ec2_remote_facts:
filters:
instance-state-name: running
"tag:Name": Example
# Gather facts about instance i-123456
- ec2_remote_facts:
filters:
instance-id: i-123456
# Gather facts about all instances in vpc-123456 that are t2.small type
- ec2_remote_facts:
filters:
vpc-id: vpc-123456
instance-type: t2.small
''' '''
try: try:
import boto
import boto.ec2 import boto.ec2
from boto.exception import BotoServerError
HAS_BOTO = True HAS_BOTO = True
except ImportError: except ImportError:
HAS_BOTO = False HAS_BOTO = False
def todict(obj, classkey=None): def get_instance_info(instance):
if isinstance(obj, dict):
data = {}
for (k, v) in obj.items():
data[k] = todict(v, classkey)
return data
elif hasattr(obj, "_ast"):
return todict(obj._ast())
elif hasattr(obj, "__iter__"):
return [todict(v, classkey) for v in obj]
elif hasattr(obj, "__dict__"):
# This Class causes a recursive loop and at this time is not worth
# debugging. If it's useful later I'll look into it.
if not isinstance(obj, boto.ec2.blockdevicemapping.BlockDeviceType):
data = dict([(key, todict(value, classkey))
for key, value in obj.__dict__.iteritems()
if not callable(value) and not key.startswith('_')])
if classkey is not None and hasattr(obj, "__class__"):
data[classkey] = obj.__class__.__name__
return data
else:
return obj
def get_all_ec2_regions(module): # Get groups
try: groups = []
regions = boto.ec2.regions() for group in instance.groups:
except Exception, e: groups.append({ 'id': group.id, 'name': group.name }.copy())
module.fail_json('Boto authentication issue: %s' % e)
# Get interfaces
interfaces = []
for interface in instance.interfaces:
interfaces.append({ 'id': interface.id, 'mac_address': interface.mac_address }.copy())
instance_info = { 'id': instance.id,
'kernel': instance.kernel,
'instance_profile': instance.instance_profile,
'root_device_type': instance.root_device_type,
'private_dns_name': instance.private_dns_name,
'public_dns_name': instance.public_dns_name,
'ebs_optimized': instance.ebs_optimized,
'client_token': instance.client_token,
'virtualization_type': instance.virtualization_type,
'architecture': instance.architecture,
'ramdisk': instance.ramdisk,
'tags': instance.tags,
'key_name': instance.key_name,
'source_destination_check': instance.sourceDestCheck,
'image_id': instance.image_id,
'groups': groups,
'interfaces': interfaces,
'spot_instance_request_id': instance.spot_instance_request_id,
'requester_id': instance.requester_id,
'monitoring_state': instance.monitoring_state,
'placement': {
'tenancy': instance._placement.tenancy,
'zone': instance._placement.zone
},
'ami_launch_index': instance.ami_launch_index,
'launch_time': instance.launch_time,
'hypervisor': instance.hypervisor,
'region': instance.region.name,
'persistent': instance.persistent,
'private_ip_address': instance.private_ip_address,
'state': instance._state.name,
'vpc_id': instance.vpc_id,
}
return instance_info
def list_ec2_instances(connection, module):
return regions filters = module.params.get("filters")
instance_dict_array = []
# Connect to ec2 region
def connect_to_region(region, module):
try: try:
conn = boto.ec2.connect_to_region(region.name) all_instances = connection.get_only_instances(filters=filters)
except Exception, e: except BotoServerError as e:
print module.jsonify('error connecting to region: ' + region.name) module.fail_json(msg=e.message)
conn = None
# connect_to_region will fail "silently" by returning for instance in all_instances:
# None if the region name is wrong or not supported instance_dict_array.append(get_instance_info(instance))
return conn
module.exit_json(instances=instance_dict_array)
def main(): def main():
module = AnsibleModule( argument_spec = ec2_argument_spec()
argument_spec = dict( argument_spec.update(
key = dict(default='Name'), dict(
value = dict(), filters = dict(default=None, type='dict')
lookup = dict(default='tags'),
ignore_state = dict(default='terminated'),
region = dict(),
) )
) )
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO: if not HAS_BOTO:
module.fail_json(msg='boto required for this module') module.fail_json(msg='boto required for this module')
server_info = list() region, ec2_url, aws_connect_params = get_aws_connection_info(module)
for region in get_all_ec2_regions(module): if region:
conn = connect_to_region(region, module)
try: try:
# Run when looking up by tag names, only returning hostname currently connection = connect_to_aws(boto.ec2, region, **aws_connect_params)
if module.params.get('lookup') == 'tags': except (boto.exception.NoAuthHandlerFound, StandardError), e:
ec2_key = 'tag:' + module.params.get('key') module.fail_json(msg=str(e))
ec2_value = module.params.get('value') else:
reservations = conn.get_all_instances(filters={ec2_key : ec2_value}) module.fail_json(msg="region must be specified")
for instance in [i for r in reservations for i in r.instances]:
if instance.private_ip_address != None: list_ec2_instances(connection, module)
instance.hostname = 'ip-' + instance.private_ip_address.replace('.', '-')
if instance._state.name not in module.params.get('ignore_state'):
server_info.append(todict(instance))
except:
print module.jsonify('error getting instances from: ' + region.name)
ec2_facts_result = dict(changed=True, ec2=server_info)
module.exit_json(**ec2_facts_result)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import * from ansible.module_utils.ec2 import *
main() if __name__ == '__main__':
main()

Loading…
Cancel
Save