# executable custom fact

**URL:** <https://forum.ansible.com/t/executable-custom-fact/13524>\
**Category:** Ansible Project\
**Created:** [September 22, 2013, 3:48pm UTC](https://forum.ansible.com/t/executable-custom-fact/13524 "2013-09-22T15:48:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [September 22, 2013, 3:48pm UTC](https://forum.ansible.com/t/executable-custom-fact/13524/1 "2013-09-22T15:48:31Z")

</div>

Hi All,

I am testing if i can do a custom fact, the static content fact either in JSON/INI file works, however if i add another file to the /etc/ansible/facts.d directory to echo the similar content it is not working some experiments there is an error reading the file, sometimes it just ignores the file completely. what am i doing wrong?

iMac:~ walid$ ls -ld /etc/ansible/facts.d/walid2.facts  
-rwxr-xr-x 1 root wheel 127 Sep 22 18:41 /etc/ansible/facts.d/walid2.facts

iMac:~ walid$ /etc/ansible/facts.d/walid2.facts  
[testing\_facts]  
string\_test=“this is a string test”  
num\_test=398

iMac:~ walid$ cat /etc/ansible/facts.d/walid2.facts  
#!/bin/bash

PSCOUNT=`ps auxw|wc -l`  
echo “[testing\_facts]”  
echo ‘string\_test=“this is a string test”’  
echo ‘num\_test=398’

tesing it using ansible -m setup and i tried both ansible 1.3 and latest development 1.4 snapshot

TIA

Walid

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 22, 2013, 7:30pm UTC](https://forum.ansible.com/t/executable-custom-fact/13524/2 "2013-09-22T19:30:44Z")

</div>

[http://www.ansibleworks.com/docs/playbooks2.html#local-facts-facts-d](http://www.ansibleworks.com/docs/playbooks2.html#local-facts-facts-d)

“If a remotely managed system has an “/etc/ansible/facts.d” directory, any files in this directory ending in ”.fact”, can be JSON, INI, or executable files returning JSON, and these can supply local facts in Ansible.”

Looks like the above is not JSON.

---

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [September 22, 2013, 8:23pm UTC](https://forum.ansible.com/t/executable-custom-fact/13524/3 "2013-09-22T20:23:31Z")

</div>

Hi Michael,

I did try with JSON format initially. my main mistake is the name of the file extension facts " with an extra s" instead of fact and then not following standard JSON format throughly the initial, and end curly bracket, now it works when i do as below :

iMac:ansible-wkshp walid$ ls -ld /etc/ansible/facts.d/walid.fact  
-rwxr-xr-x 1 walid staff 102 Sep 22 23:20 /etc/ansible/facts.d/walid.fact

iMac:ansible-wkshp walid$ /etc/ansible/facts.d/walid.fact  
{“testing\_facts”: {“test\_2”: 100, “test\_1”: 1}}

---

<div class="post-metadata">

**Author:** ![sandra.kosmalla](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@sandra.kosmalla](https://forum.ansible.com/u/sandra.kosmalla)\
**Post date:** [November 14, 2014, 7:37am UTC](https://forum.ansible.com/t/executable-custom-fact/13524/4 "2014-11-14T07:37:13Z")

</div>

Hi,

I start learning Ansible with a Puppet background, so the concept of executable facts is well-known. Sometimes it is easier to write a little shell script for a custom fact, so my question is, is it planned to support INI format as output for executable facts? It would make easier writing custom facts in shell.

Regards,

Sandra

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [November 16, 2014, 5:09pm UTC](https://forum.ansible.com/t/executable-custom-fact/13524/5 "2014-11-16T17:09:24Z")

</div>

Michael can correct me if I'm wrong but I believe we're moving more  
and more to json for communicating between modules and ansible core.  
So we probably wouldn't be adding additional use of ini at that level.  
Note that for simple return values, (which is hopefully all you're  
doing with ini 😉 json is only a little harder to generate in shell.  
Substitute something like this:

# ini:  
printf '[myfact]\n'  
for key in foo bar ; do  
&nbsp;&nbsp;for value in `seq 1 2` ; do  
&nbsp;&nbsp;&nbsp;&nbsp;printf '%s%s=%s\n' $key $value $value  
&nbsp;&nbsp;done  
done

with this:  
# json  
output='{ "myfact": { '  
for key in foo bar ; do  
&nbsp;&nbsp;for value in `seq 1 2` ; do  
&nbsp;&nbsp;&nbsp;&nbsp;output+=`printf '"%s%s":%s,' $key $value $value`  
&nbsp;&nbsp;done  
done  
output=`echo $output | sed 's/,$//'`  
printf '%s } }\n' "$output"

JSON has slightly more verbosity (the curly braces to mark where the  
data structures begin and end) and you have to add a bit of logic to  
make sure your sequence of values does not end with a comma but it's  
not too bad. Nothing like \*shudder\* writing XML in shell 😉

-Toshio

---

<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:** [November 17, 2014, 8:50pm UTC](https://forum.ansible.com/t/executable-custom-fact/13524/6 "2014-11-17T20:50:33Z")

</div>

INI files are presently supported for local facts and are parsed with Python’s ConfigParser.

---

<div class="post-metadata">

**Author:** ![sandra.kosmalla](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@sandra.kosmalla](https://forum.ansible.com/u/sandra.kosmalla)\
**Post date:** [November 18, 2014, 12:00pm UTC](https://forum.ansible.com/t/executable-custom-fact/13524/7 "2014-11-18T12:00:02Z")

</div>

Thank you for the background information and for the code snippet.
