# Use custom fact module

**URL:** <https://forum.ansible.com/t/use-custom-fact-module/15586>\
**Category:** Ansible Project\
**Created:** [July 1, 2014, 5:16pm UTC](https://forum.ansible.com/t/use-custom-fact-module/15586 "2014-07-01T17:16:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Olliver](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Mark\_Olliver](https://forum.ansible.com/u/Mark_Olliver)\
**Post date:** [July 1, 2014, 5:16pm UTC](https://forum.ansible.com/t/use-custom-fact-module/15586/1 "2014-07-01T17:16:57Z")

</div>

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/](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:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 1, 2014, 6:39pm UTC](https://forum.ansible.com/t/use-custom-fact-module/15586/2 "2014-07-01T18:39:42Z")

</div>

Hi, this is a topic for ansible-devel, can you please ask over there? Thanks!
