2013-03-19 17:07:36 +01:00
|
|
|
#!/usr/bin/python
|
2013-01-22 21:31:51 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-05-15 17:32:35 +02:00
|
|
|
DOCUMENTATION = '''
|
2013-01-22 21:31:51 +01:00
|
|
|
---
|
|
|
|
module: ec2_facts
|
|
|
|
short_description: Gathers facts about remote hosts within ec2 (aws)
|
2013-04-17 02:07:18 +02:00
|
|
|
version_added: "1.0"
|
2014-03-12 16:19:22 +01:00
|
|
|
options:
|
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices: ['yes', 'no']
|
|
|
|
version_added: 1.5.1
|
2013-02-01 03:00:25 +01:00
|
|
|
description:
|
2014-09-23 00:48:23 +02:00
|
|
|
- This module fetches data from the metadata servers in ec2 (aws) as per
|
|
|
|
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html.
|
|
|
|
The module must be called from within the EC2 instance itself.
|
2013-02-01 03:00:25 +01:00
|
|
|
notes:
|
2013-01-23 15:39:26 +01:00
|
|
|
- Parameters to filter on ec2_facts may be added later.
|
2013-01-26 18:34:30 +01:00
|
|
|
author: "Silviu Dicu <silviudicu@gmail.com>"
|
2013-05-15 17:32:35 +02:00
|
|
|
'''
|
2013-01-22 21:31:51 +01:00
|
|
|
|
2013-05-15 17:32:35 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Conditional example
|
|
|
|
- name: Gather facts
|
|
|
|
action: ec2_facts
|
|
|
|
|
|
|
|
- name: Conditional
|
|
|
|
action: debug msg="This instance is a t1.micro"
|
|
|
|
when: ansible_ec2_instance_type == "t1.micro"
|
|
|
|
'''
|
|
|
|
|
2013-01-22 21:31:51 +01:00
|
|
|
import socket
|
2013-01-25 01:14:32 +01:00
|
|
|
import re
|
2013-01-22 21:31:51 +01:00
|
|
|
|
|
|
|
socket.setdefaulttimeout(5)
|
|
|
|
|
|
|
|
class Ec2Metadata(object):
|
2013-02-01 03:00:25 +01:00
|
|
|
|
2013-01-25 01:14:32 +01:00
|
|
|
ec2_metadata_uri = 'http://169.254.169.254/latest/meta-data/'
|
|
|
|
ec2_sshdata_uri = 'http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key'
|
|
|
|
ec2_userdata_uri = 'http://169.254.169.254/latest/user-data/'
|
2013-01-22 21:31:51 +01:00
|
|
|
|
2013-04-07 04:57:03 +02:00
|
|
|
AWS_REGIONS = ('ap-northeast-1',
|
|
|
|
'ap-southeast-1',
|
|
|
|
'ap-southeast-2',
|
|
|
|
'eu-west-1',
|
|
|
|
'sa-east-1',
|
|
|
|
'us-east-1',
|
|
|
|
'us-west-1',
|
2014-08-28 04:06:15 +02:00
|
|
|
'us-west-2',
|
|
|
|
'us-gov-west-1'
|
|
|
|
)
|
2013-04-07 04:57:03 +02:00
|
|
|
|
2014-03-10 22:06:52 +01:00
|
|
|
def __init__(self, module, ec2_metadata_uri=None, ec2_sshdata_uri=None, ec2_userdata_uri=None):
|
|
|
|
self.module = module
|
2013-01-25 01:14:32 +01:00
|
|
|
self.uri_meta = ec2_metadata_uri or self.ec2_metadata_uri
|
|
|
|
self.uri_user = ec2_userdata_uri or self.ec2_userdata_uri
|
|
|
|
self.uri_ssh = ec2_sshdata_uri or self.ec2_sshdata_uri
|
2013-02-01 03:00:25 +01:00
|
|
|
self._data = {}
|
|
|
|
self._prefix = 'ansible_ec2_%s'
|
2013-01-22 21:31:51 +01:00
|
|
|
|
|
|
|
def _fetch(self, url):
|
2014-03-10 22:06:52 +01:00
|
|
|
(response, info) = fetch_url(self.module, url, force=True)
|
2014-03-12 02:38:00 +01:00
|
|
|
if response:
|
|
|
|
data = response.read()
|
|
|
|
else:
|
|
|
|
data = None
|
|
|
|
return data
|
2013-01-22 21:31:51 +01:00
|
|
|
|
2013-01-25 01:14:32 +01:00
|
|
|
def _mangle_fields(self, fields, uri, filter_patterns=['public-keys-0']):
|
|
|
|
new_fields = {}
|
|
|
|
for key, value in fields.iteritems():
|
|
|
|
split_fields = key[len(uri):].split('/')
|
|
|
|
if len(split_fields) > 1 and split_fields[1]:
|
|
|
|
new_key = "-".join(split_fields)
|
|
|
|
new_fields[self._prefix % new_key] = value
|
2013-01-22 21:31:51 +01:00
|
|
|
else:
|
2013-01-25 01:14:32 +01:00
|
|
|
new_key = "".join(split_fields)
|
|
|
|
new_fields[self._prefix % new_key] = value
|
|
|
|
for pattern in filter_patterns:
|
|
|
|
for key in new_fields.keys():
|
|
|
|
match = re.search(pattern, key)
|
2013-02-18 01:48:02 +01:00
|
|
|
if match:
|
|
|
|
new_fields.pop(key)
|
2013-01-25 01:14:32 +01:00
|
|
|
return new_fields
|
|
|
|
|
|
|
|
def fetch(self, uri, recurse=True):
|
|
|
|
raw_subfields = self._fetch(uri)
|
|
|
|
if not raw_subfields:
|
|
|
|
return
|
|
|
|
subfields = raw_subfields.split('\n')
|
|
|
|
for field in subfields:
|
|
|
|
if field.endswith('/') and recurse:
|
|
|
|
self.fetch(uri + field)
|
|
|
|
if uri.endswith('/'):
|
|
|
|
new_uri = uri + field
|
|
|
|
else:
|
|
|
|
new_uri = uri + '/' + field
|
|
|
|
if new_uri not in self._data and not new_uri.endswith('/'):
|
|
|
|
content = self._fetch(new_uri)
|
|
|
|
if field == 'security-groups':
|
|
|
|
sg_fields = ",".join(content.split('\n'))
|
|
|
|
self._data['%s' % (new_uri)] = sg_fields
|
|
|
|
else:
|
|
|
|
self._data['%s' % (new_uri)] = content
|
|
|
|
|
2013-02-01 03:00:25 +01:00
|
|
|
def fix_invalid_varnames(self, data):
|
|
|
|
"""Change ':'' and '-' to '_' to ensure valid template variable names"""
|
|
|
|
for (key, value) in data.items():
|
|
|
|
if ':' in key or '-' in key:
|
|
|
|
newkey = key.replace(':','_').replace('-','_')
|
2014-04-24 01:12:55 +02:00
|
|
|
del data[key]
|
2013-02-01 03:00:25 +01:00
|
|
|
data[newkey] = value
|
|
|
|
|
2013-04-07 04:57:03 +02:00
|
|
|
def add_ec2_region(self, data):
|
|
|
|
"""Use the 'ansible_ec2_placement_availability_zone' key/value
|
|
|
|
pair to add 'ansible_ec2_placement_region' key/value pair with
|
|
|
|
the EC2 region name.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Only add a 'ansible_ec2_placement_region' key if the
|
|
|
|
# 'ansible_ec2_placement_availability_zone' exists.
|
|
|
|
zone = data.get('ansible_ec2_placement_availability_zone')
|
|
|
|
if zone is not None:
|
|
|
|
# Use the zone name as the region name unless the zone
|
|
|
|
# name starts with a known AWS region name.
|
|
|
|
region = zone
|
|
|
|
for r in self.AWS_REGIONS:
|
|
|
|
if zone.startswith(r):
|
|
|
|
region = r
|
|
|
|
break
|
|
|
|
data['ansible_ec2_placement_region'] = region
|
|
|
|
|
2013-01-25 01:14:32 +01:00
|
|
|
def run(self):
|
|
|
|
self.fetch(self.uri_meta) # populate _data
|
2013-02-18 01:48:02 +01:00
|
|
|
data = self._mangle_fields(self._data, self.uri_meta)
|
2013-01-25 01:14:32 +01:00
|
|
|
data[self._prefix % 'user-data'] = self._fetch(self.uri_user)
|
|
|
|
data[self._prefix % 'public-key'] = self._fetch(self.uri_ssh)
|
2013-02-01 03:00:25 +01:00
|
|
|
self.fix_invalid_varnames(data)
|
2013-04-07 04:57:03 +02:00
|
|
|
self.add_ec2_region(data)
|
2013-01-22 21:31:51 +01:00
|
|
|
return data
|
|
|
|
|
|
|
|
def main():
|
2014-03-10 22:06:52 +01:00
|
|
|
argument_spec = url_argument_spec()
|
2013-02-18 01:48:02 +01:00
|
|
|
|
2013-01-22 21:31:51 +01:00
|
|
|
module = AnsibleModule(
|
2014-03-10 22:06:52 +01:00
|
|
|
argument_spec = argument_spec,
|
2013-07-12 02:42:15 +02:00
|
|
|
supports_check_mode = True,
|
2013-01-22 21:31:51 +01:00
|
|
|
)
|
2014-03-10 22:06:52 +01:00
|
|
|
|
|
|
|
ec2_facts = Ec2Metadata(module).run()
|
|
|
|
ec2_facts_result = dict(changed=False, ansible_facts=ec2_facts)
|
|
|
|
|
2013-01-22 21:31:51 +01:00
|
|
|
module.exit_json(**ec2_facts_result)
|
|
|
|
|
2013-11-01 16:59:24 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2014-03-10 22:06:52 +01:00
|
|
|
from ansible.module_utils.urls import *
|
2013-01-22 21:31:51 +01:00
|
|
|
|
|
|
|
main()
|