HI,
I have written a custom fact module to retrieve metadata from Google Compute Engine hosts. The script itself works fine and returns valid json however, I can not see how I call that within Ansible so that I can make use of the fact later.
My script is get_metadata.py
#!/usr/bin/env python
import urllib2
import sys
import json
url = “http://metadata.google.internal/computeMetadata/v1/instance/attributes/”
headers = { “Metadata-Flavor” : “Google” }
metadata = [‘dc’]
content = {}
for x in range (0, len(metadata)):
req = urllib2.Request(url + metadata, None, headers)
try:
response = urllib2.urlopen(req)
content[metadata] = (response.read())
except urllib2.HTTPError, e:
pass
result = ‘{ “ansible_facts”: ’ + json.dumps(content) + ’ }’
print result
The ansible command I am using is: ansible-playbook -s -i plugins/inventory/gce.py -M /opt/ansible/modules/ deploy.yml
with my module stored in /opt/ansible/modules/get_metadata.py
Deploy.yml looks like this: