|
|
@ -24,32 +24,36 @@ short_description: Gathers facts about remote hosts within ec2 (aws)
|
|
|
|
options: {}
|
|
|
|
options: {}
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- This module fetches data from the metadata servers in ec2 (aws).
|
|
|
|
- This module fetches data from the metadata servers in ec2 (aws).
|
|
|
|
|
|
|
|
Eucalyptus cloud provides a similar service and this module should
|
|
|
|
|
|
|
|
work this cloud provider as well.
|
|
|
|
notes:
|
|
|
|
notes:
|
|
|
|
- Parameters to filter on ec2_facts may be added later.
|
|
|
|
- Parameters to filter on ec2_facts may be added later.
|
|
|
|
Some of the facts are not returned ( like mapping of the devices - but
|
|
|
|
|
|
|
|
may be added later).
|
|
|
|
|
|
|
|
examples:
|
|
|
|
examples:
|
|
|
|
- code: ansible all -m ec2_facts --tree /tmp/facts
|
|
|
|
- code: ansible all -m ec2_facts --tree /tmp/facts
|
|
|
|
description: Obtain facts from ec2 metatdata servers. You will need to
|
|
|
|
description: Obtain facts from ec2 metatdata servers. You will need to
|
|
|
|
run an instance within ec2.
|
|
|
|
run an instance within ec2.
|
|
|
|
author: Silviu Dicu
|
|
|
|
|
|
|
|
|
|
|
|
author: Silviu Dicu: silviudicu@gmail.com
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
import urllib2
|
|
|
|
import urllib2
|
|
|
|
import socket
|
|
|
|
import socket
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
socket.setdefaulttimeout(5)
|
|
|
|
socket.setdefaulttimeout(5)
|
|
|
|
|
|
|
|
|
|
|
|
class Ec2Metadata(object):
|
|
|
|
class Ec2Metadata(object):
|
|
|
|
|
|
|
|
|
|
|
|
ec2_metadata_url = 'http://169.254.169.254/latest/meta-data/'
|
|
|
|
ec2_metadata_uri = 'http://169.254.169.254/latest/meta-data/'
|
|
|
|
ec2_sshdata_url = 'http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key'
|
|
|
|
ec2_sshdata_uri = 'http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key'
|
|
|
|
ec2_userdata_url = 'http://169.254.169.254/latest/user-data/'
|
|
|
|
ec2_userdata_uri = 'http://169.254.169.254/latest/user-data/'
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, ec2_metadata_url=None, ec2_sshdata_url=None, ec2_userdata_url=None):
|
|
|
|
def __init__(self, ec2_metadata_uri=None, ec2_sshdata_uri=None, ec2_userdata_uri=None):
|
|
|
|
self.url_meta = ec2_metadata_url or self.ec2_metadata_url
|
|
|
|
self.uri_meta = ec2_metadata_uri or self.ec2_metadata_uri
|
|
|
|
self.url_user = ec2_userdata_url or self.ec2_userdata_url
|
|
|
|
self.uri_user = ec2_userdata_uri or self.ec2_userdata_uri
|
|
|
|
self.url_ssh = ec2_sshdata_url or self.ec2_sshdata_url
|
|
|
|
self.uri_ssh = ec2_sshdata_uri or self.ec2_sshdata_uri
|
|
|
|
|
|
|
|
self._data = {}
|
|
|
|
|
|
|
|
self._prefix = 'ansible_ec2_%s'
|
|
|
|
|
|
|
|
|
|
|
|
def _fetch(self, url):
|
|
|
|
def _fetch(self, url):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
@ -59,22 +63,48 @@ class Ec2Metadata(object):
|
|
|
|
except urllib2.URLError:
|
|
|
|
except urllib2.URLError:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def run(self, field=None):
|
|
|
|
def _mangle_fields(self, fields, uri, filter_patterns=['public-keys-0']):
|
|
|
|
data = {}
|
|
|
|
new_fields = {}
|
|
|
|
raw_fields = self._fetch(self.url_meta)
|
|
|
|
for key, value in fields.iteritems():
|
|
|
|
if not raw_fields:
|
|
|
|
split_fields = key[len(uri):].split('/')
|
|
|
|
return data
|
|
|
|
if len(split_fields) > 1 and split_fields[1]:
|
|
|
|
fields = raw_fields.split('\n')
|
|
|
|
new_key = "-".join(split_fields)
|
|
|
|
for field in fields:
|
|
|
|
new_fields[self._prefix % new_key] = value
|
|
|
|
if field.endswith('/'): continue # deal with this later
|
|
|
|
else:
|
|
|
|
field_data = self._fetch(self.url_meta + field)
|
|
|
|
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)
|
|
|
|
|
|
|
|
if match: new_fields.pop(key)
|
|
|
|
|
|
|
|
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':
|
|
|
|
if field == 'security-groups':
|
|
|
|
sg_fields = ",".join(field_data.split('\n'))
|
|
|
|
sg_fields = ",".join(content.split('\n'))
|
|
|
|
data['ansible_ec2_%s' % field] = sg_fields
|
|
|
|
self._data['%s' % (new_uri)] = sg_fields
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
data['ansible_ec2_%s' % field] = field_data
|
|
|
|
self._data['%s' % (new_uri)] = content
|
|
|
|
data['ansible_ec2_%s' % 'user-data'] = self._fetch(self.url_user)
|
|
|
|
|
|
|
|
data['ensible_ec2_%s' % 'public-keys'] = self._fetch(self.url_ssh)
|
|
|
|
def run(self):
|
|
|
|
|
|
|
|
self.fetch(self.uri_meta) # populate _data
|
|
|
|
|
|
|
|
data = self._mangle_fields(self._data,
|
|
|
|
|
|
|
|
self.uri_meta)
|
|
|
|
|
|
|
|
data[self._prefix % 'user-data'] = self._fetch(self.uri_user)
|
|
|
|
|
|
|
|
data[self._prefix % 'public-key'] = self._fetch(self.uri_ssh)
|
|
|
|
return data
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|