1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

If ec2 fact has ':' or '-', change to '_'

Change ec2 fact names with colons (:) or dashes (:) to undescore (_)
so that they can be used in templates.

Note that this makes copies instead of replacing the exisitng fact
names to avoid breaking existing playbooks.
This commit is contained in:
Lorin Hochstein 2013-01-31 21:00:25 -05:00
parent f7e286cf34
commit 4d38c68489

View file

@ -21,11 +21,11 @@ DOCUMENTATION="""
module: ec2_facts module: ec2_facts
short_description: Gathers facts about remote hosts within ec2 (aws) 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 Eucalyptus cloud provides a similar service and this module should
work this cloud provider as well. 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.
examples: examples:
- code: ansible all -m ec2_facts - code: ansible all -m ec2_facts
@ -40,7 +40,7 @@ import re
socket.setdefaulttimeout(5) socket.setdefaulttimeout(5)
class Ec2Metadata(object): class Ec2Metadata(object):
ec2_metadata_uri = 'http://169.254.169.254/latest/meta-data/' 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_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/' ec2_userdata_uri = 'http://169.254.169.254/latest/user-data/'
@ -49,8 +49,8 @@ class Ec2Metadata(object):
self.uri_meta = ec2_metadata_uri or self.ec2_metadata_uri self.uri_meta = ec2_metadata_uri or self.ec2_metadata_uri
self.uri_user = ec2_userdata_uri or self.ec2_userdata_uri self.uri_user = ec2_userdata_uri or self.ec2_userdata_uri
self.uri_ssh = ec2_sshdata_uri or self.ec2_sshdata_uri self.uri_ssh = ec2_sshdata_uri or self.ec2_sshdata_uri
self._data = {} self._data = {}
self._prefix = 'ansible_ec2_%s' self._prefix = 'ansible_ec2_%s'
def _fetch(self, url): def _fetch(self, url):
try: try:
@ -96,18 +96,27 @@ class Ec2Metadata(object):
else: else:
self._data['%s' % (new_uri)] = content self._data['%s' % (new_uri)] = content
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('-','_')
data[newkey] = value
def run(self): def run(self):
self.fetch(self.uri_meta) # populate _data self.fetch(self.uri_meta) # populate _data
data = self._mangle_fields(self._data, data = self._mangle_fields(self._data,
self.uri_meta) self.uri_meta)
data[self._prefix % 'user-data'] = self._fetch(self.uri_user) data[self._prefix % 'user-data'] = self._fetch(self.uri_user)
data[self._prefix % 'public-key'] = self._fetch(self.uri_ssh) data[self._prefix % 'public-key'] = self._fetch(self.uri_ssh)
self.fix_invalid_varnames(data)
return data return data
def main(): def main():
ec2_facts = Ec2Metadata().run() ec2_facts = Ec2Metadata().run()
ec2_facts_result = { ec2_facts_result = {
"changed" : False, "changed" : False,
"ansible_facts" : ec2_facts "ansible_facts" : ec2_facts
} }