Last active
May 28, 2017 19:30
-
-
Save michaeljs1990/9a8213c4f300e27aba2d to your computer and use it in GitHub Desktop.
python ??
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
False | |
<type 'NoneType'> | |
kernel | |
False | |
<type 'NoneType'> | |
instance_profile | |
False | |
<type 'unicode'> | |
root_device_type | |
False | |
<type 'unicode'> | |
private_dns_name | |
False | |
<class 'boto.ec2.instance.InstanceState'> | |
_state | |
False | |
<type 'NoneType'> | |
group_name | |
False | |
<type 'str'> | |
public_dns_name | |
False | |
<type 'unicode'> | |
id | |
False | |
<type 'NoneType'> | |
state_reason | |
False | |
<type 'bool'> | |
monitored | |
False | |
<type 'unicode'> | |
item | |
False | |
<type 'unicode'> | |
subnet_id | |
False | |
<class 'boto.ec2.blockdevicemapping.BlockDeviceMapping'> | |
block_device_mapping | |
False | |
<type 'NoneType'> | |
platform | |
False | |
<type 'NoneType'> | |
eventsSet | |
False | |
<type 'bool'> | |
ebs_optimized | |
False | |
<type 'str'> | |
client_token | |
False | |
<type 'bool'> | |
_in_monitoring_element | |
False | |
<type 'unicode'> | |
virtualization_type | |
False | |
<type 'unicode'> | |
architecture | |
False | |
<type 'NoneType'> | |
ramdisk | |
False | |
<type 'NoneType'> | |
_previous_state | |
False | |
<class 'boto.ec2.tag.TagSet'> | |
tags | |
False | |
<type 'unicode'> | |
key_name | |
False | |
<class 'boto.resultset.ResultSet'> | |
interfaces | |
False | |
<type 'unicode'> | |
sourceDestCheck | |
False | |
<type 'unicode'> | |
image_id | |
False | |
<type 'str'> | |
reason | |
False | |
<class 'boto.resultset.ResultSet'> | |
groups | |
False | |
<type 'NoneType'> | |
spot_instance_request_id | |
False | |
<type 'unicode'> | |
monitoring | |
False | |
<type 'NoneType'> | |
requester_id | |
False | |
<type 'unicode'> | |
ip_address | |
False | |
<type 'unicode'> | |
monitoring_state | |
False | |
<class 'boto.ec2.instance.InstancePlacement'> | |
_placement | |
False | |
<type 'unicode'> | |
ami_launch_index | |
False | |
<type 'str'> | |
dns_name | |
False | |
<class 'boto.regioninfo.RegionInfo'> | |
region | |
False | |
<type 'unicode'> | |
launch_time | |
False | |
<type 'bool'> | |
persistent | |
False | |
<type 'unicode'> | |
instance_type | |
False | |
<class 'boto.ec2.connection.EC2Connection'> | |
connection | |
False | |
<type 'unicode'> | |
root_device_name | |
False | |
<type 'unicode'> | |
hypervisor | |
False | |
<type 'unicode'> | |
private_ip_address | |
False | |
<type 'unicode'> | |
vpc_id | |
False | |
<class 'boto.ec2.image.ProductCodes'> | |
product_codes | |
False | |
<type 'unicode'> | |
hostname | |
False | |
<type 'NoneType'> | |
kernel | |
False | |
<type 'NoneType'> | |
instance_profile | |
False | |
<type 'unicode'> | |
root_device_type | |
False | |
<type 'unicode'> | |
private_dns_name | |
False | |
<class 'boto.ec2.instance.InstanceState'> | |
_state | |
False | |
<type 'NoneType'> | |
group_name | |
False | |
<type 'unicode'> | |
public_dns_name | |
False | |
<type 'unicode'> | |
id | |
False | |
<type 'NoneType'> | |
state_reason | |
False | |
<type 'bool'> | |
monitored | |
False | |
<type 'unicode'> | |
item | |
False | |
<type 'unicode'> | |
subnet_id | |
False | |
<class 'boto.ec2.blockdevicemapping.BlockDeviceMapping'> | |
block_device_mapping | |
False | |
<type 'NoneType'> | |
platform | |
False | |
<type 'NoneType'> | |
eventsSet | |
False | |
<type 'bool'> | |
ebs_optimized | |
False | |
<type 'unicode'> | |
client_token | |
False | |
<type 'bool'> | |
_in_monitoring_element | |
False | |
<type 'unicode'> | |
virtualization_type | |
False | |
<type 'unicode'> | |
architecture | |
False | |
<type 'NoneType'> | |
ramdisk | |
False | |
<type 'NoneType'> | |
_previous_state | |
False | |
<class 'boto.ec2.tag.TagSet'> | |
tags | |
False | |
<type 'unicode'> | |
key_name | |
False | |
<class 'boto.resultset.ResultSet'> | |
interfaces | |
False | |
<type 'unicode'> | |
sourceDestCheck | |
False | |
<type 'unicode'> | |
image_id | |
False | |
<type 'str'> | |
reason | |
False | |
<class 'boto.resultset.ResultSet'> | |
groups | |
False | |
<type 'NoneType'> | |
spot_instance_request_id | |
False | |
<type 'unicode'> | |
monitoring | |
False | |
<type 'NoneType'> | |
requester_id | |
False | |
<type 'unicode'> | |
ip_address | |
False | |
<type 'unicode'> | |
monitoring_state | |
False | |
<class 'boto.ec2.instance.InstancePlacement'> | |
_placement | |
False | |
<type 'unicode'> | |
ami_launch_index | |
False | |
<type 'unicode'> | |
dns_name | |
False | |
<class 'boto.regioninfo.RegionInfo'> | |
region | |
False | |
<type 'unicode'> | |
launch_time | |
False | |
<type 'bool'> | |
persistent | |
False | |
<type 'unicode'> | |
instance_type | |
False | |
<class 'boto.ec2.connection.EC2Connection'> | |
connection | |
False | |
<type 'unicode'> | |
root_device_name | |
False | |
<type 'unicode'> | |
hypervisor | |
False | |
<type 'unicode'> | |
private_ip_address | |
False | |
<type 'unicode'> | |
vpc_id | |
False | |
<class 'boto.ec2.image.ProductCodes'> | |
product_codes | |
False | |
<type 'unicode'> | |
hostname |
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
#!/usr/bin/python | |
# This file is part of NoWait | |
DOCUMENTATION = ''' | |
--- | |
module: ec2_search | |
short_description: ask EC2 for information about other instances. | |
description: | |
- Only supports seatch for hostname by tags currently. Looking to add more later. | |
version_added: "1.9" | |
options: | |
key: | |
description: | |
- instance tag key in EC2 | |
required: false | |
default: Name | |
aliases: [] | |
value: | |
description: | |
- instance tag value in EC2 | |
required: false | |
default: null | |
aliases: [] | |
lookup: | |
description: | |
- What type of lookup to use when searching EC2 instance info. | |
required: false | |
default: tags | |
aliases: [] | |
region: | |
description: | |
- EC2 region that it should look for tags in | |
required: false | |
default: All Regions | |
aliases: [] | |
author: | |
- "Michael Schuett (@michaeljs1990)" | |
extends_documentation_fragment: aws | |
''' | |
EXAMPLES = ''' | |
# Note: These examples do not set authentication details, see the AWS Guide for details. | |
# Basic provisioning example | |
- ec2_search: | |
key: mykey | |
value: myvalue | |
''' | |
try: | |
import boto | |
import boto.ec2 | |
HAS_BOTO = True | |
except ImportError: | |
HAS_BOTO = False | |
def get_all_ec2_regions(module): | |
try: | |
regions = boto.ec2.regions() | |
except Exception, e: | |
module.fail_json('Boto authentication issue: %s' % e) | |
return regions | |
# Connect to ec2 region | |
def connect_to_region(region, module): | |
try: | |
conn = boto.ec2.connect_to_region(region.name) | |
except Exception, e: | |
print module.jsonify('error connecting to region: ' + region.name) | |
conn = None | |
# connect_to_region will fail "silently" by returning | |
# None if the region name is wrong or not supported | |
return conn | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
key = dict(default='Name'), | |
value = dict(), | |
lookup = dict(default='tags'), | |
region = dict(), | |
) | |
) | |
if not HAS_BOTO: | |
module.fail_json(msg='boto required for this module') | |
server_info = list() | |
for region in get_all_ec2_regions(module): | |
conn = connect_to_region(region, module) | |
try: | |
# Run when looking up by tag names, only returning hostname currently | |
if module.params.get('lookup') == 'tags': | |
ec2_key = 'tag:' + module.params.get('key') | |
ec2_value = module.params.get('value') | |
reservations = conn.get_all_instances(filters={ec2_key : ec2_value}) | |
for instance in [i for r in reservations for i in r.instances]: | |
instance_dict = dict() | |
if instance.private_ip_address != None: | |
instance.hostname = 'ip-' + instance.private_ip_address.replace('.', '-') | |
for (key, value) in instance.__dict__.iteritems(): | |
print inspect.isclass(value) | |
print type(value) | |
if inspect.isclass(value): | |
print key | |
instance_dict[key] = value.__dict__ | |
else: | |
instance_dict[key] = value | |
server_info.append(instance_dict) | |
except: | |
print module.jsonify('error getting instances from: ' + region.name) | |
print server_info | |
exit() | |
ansible_facts = {'ec2_search': server_info} | |
ec2_facts_result = dict(changed=True, ansible_facts=ansible_facts) | |
module.exit_json(**ec2_facts_result) | |
# import module snippets | |
from ansible.module_utils.basic import * | |
from ansible.module_utils.ec2 import * | |
import inspect | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment