executable custom fact

510 views
Skip to first unread message

Walid

unread,
Sep 22, 2013, 11:48:31 AM9/22/13
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Sep 22, 2013, 3:30:44 PM9/22/13
to ansible...@googlegroups.com

"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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Walid

unread,
Sep 22, 2013, 4:23:31 PM9/22/13
to ansible...@googlegroups.com
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}}

sandra....@googlemail.com

unread,
Nov 14, 2014, 2:37:13 AM11/14/14
to ansible...@googlegroups.com
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

Toshio Kuratomi

unread,
Nov 16, 2014, 12:09:29 PM11/16/14
to ansible...@googlegroups.com
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
for value in `seq 1 2` ; do
printf '%s%s=%s\n' $key $value $value
done
done

with this:
# json
output='{ "myfact": { '
for key in foo bar ; do
for value in `seq 1 2` ; do
output+=`printf '"%s%s":%s,' $key $value $value`
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
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/8476294d-e20f-4ab5-9277-0b18010413fd%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
Nov 17, 2014, 3:50:37 PM11/17/14
to ansible...@googlegroups.com
INI files are presently supported for local facts and are parsed with Python's ConfigParser.




sandra....@googlemail.com

unread,
Nov 18, 2014, 7:00:02 AM11/18/14
to ansible...@googlegroups.com
Thank you for the background information and for the code snippet.
Reply all
Reply to author
Forward
0 new messages