# ansible node variables

**URL:** <https://forum.ansible.com/t/ansible-node-variables/11688>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [July 17, 2012, 12:40am UTC](https://forum.ansible.com/t/ansible-node-variables/11688 "2012-07-17T00:40:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 17, 2012, 12:40am UTC](https://forum.ansible.com/t/ansible-node-variables/11688/1 "2012-07-17T00:40:51Z")

</div>

It is -- it's pretty simple.

Basically you should just write a module that returns an ansible\_facts dictionary in JSON\* like so:

{  
&nbsp;&nbsp;&nbsp;&nbsp;ansible\_facts: {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'lein' : "path\_to\_lein\_goes\_here",  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

And call it in the very first part of your playbook. Maybe your custom module just runs which -- it's up to you.

It's usually sufficient to just name this module something like "site\_facts".

\*(Technically it doesn't have to be JSON, you can return key=value pairs as a string and it will work too, should you want  
to write something in bash)

---

<div class="post-metadata">

**Author:** ![Ajax](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Ajax](https://forum.ansible.com/u/Ajax)\
**Post date:** [July 18, 2012, 12:48am UTC](https://forum.ansible.com/t/ansible-node-variables/11688/2 "2012-07-18T00:48:45Z")

</div>

Thanks Michael.

I made the following playbook and made a module named site\_facts that returns a key=value pair, but whenever I try to call the key I get the error below. It looks to me like even though the parser is working, I can’t use the output of the module. Any thoughts you might have would be welcome.

Playbook:

- hosts: development  
user: root

tasks:

- name: node facts  
action: site\_facts
- name: move files  
action: command mv $lein\_path /home/ajax

Module: site\_facts

#!/bin/bash

LEIN=`which lein`

echo “lein\_path=$LEIN”

Error:

TASK: [node facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [xxx.xxx.xxx.xxx]

TASK: [move files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [xxx.xxx.xxx.xxx] =\> {“changed”: true, “cmd”: [“mv”, “$lein\_path”, “/home/christopher”], “delta”: “0:00:00.019926”, “end”: “2012-07-17 16:10:35.427514”, “rc”: 1, “start”: “2012-07-17 16:10:35.407588”, “stderr”: “mv: cannot stat `$lein\_path’: No such file or directory”, “stdout”: “”}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 18, 2012, 1:03am UTC](https://forum.ansible.com/t/ansible-node-variables/11688/3 "2012-07-18T01:03:57Z")

</div>

What version of ansible? Many fact things on devel were fixed up last night.

Also can you run with --verbose?

– Michael

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 18, 2012, 1:42am UTC](https://forum.ansible.com/t/ansible-node-variables/11688/4 "2012-07-18T01:42:56Z")

</div>

Aah, I know the deal here, you have to return a dictionary with an ansible\_facts subdictionary, so the trivial key=value is not sufficient, you must return JSON. a simple here doc would be ok if you still want to do bash.

---

<div class="post-metadata">

**Author:** ![Ajax](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Ajax](https://forum.ansible.com/u/Ajax)\
**Post date:** [July 18, 2012, 5:56am UTC](https://forum.ansible.com/t/ansible-node-variables/11688/5 "2012-07-18T05:56:12Z")

</div>

Thanks Michael. I’m using the latest version pulled from git as of twenty minutes ago. I rewrote the module and it appears to parse correctly, but the problem remains. Playbook and module are also below

TASK: [node facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [xxx.xxx.xxx.xxx] =\> {“ansible\_facts”: {“lein\_path”: “/usr/bin/lein”}}

TASK: [move files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [xxx.xxx.xxx.xxx] =\> {“changed”: true, “cmd”: [“mv”, “${lein\_path}”, “/home/ajax”], “delta”: “0:00:00.020335”, “end”: “2012-07-17 22:43:17.236149”, “rc”: 1, “start”: “2012-07-17 22:43:17.215814”, “stderr”: “mv: cannot stat `${lein\_path}': No such file or directory”, “stdout”: “”}

Playbook:

- hosts: ajax  
user: ajax  
sudo: True

tasks:

- name: node facts  
action: site\_facts
- name: move files  
action: command mv ${lein\_path} /home/ajax

Module:

#!/bin/bash

LEIN=`which lein`

cat \<\<EOF  
{  
“ansible\_facts” : {  
“lein\_path” : “$LEIN”  
}  
}

EOF

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 18, 2012, 12:03pm UTC](https://forum.ansible.com/t/ansible-node-variables/11688/6 "2012-07-18T12:03:42Z")

</div>

I just merged a fix for fact handling from jhoekx, you might want to try to pull again, but this looks like it.

If that is still busted, let me know, and I'll test your method and fix it up tonight.

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [July 18, 2012, 12:15pm UTC](https://forum.ansible.com/t/ansible-node-variables/11688/7 "2012-07-18T12:15:40Z")

</div>

> I just merged a fix for fact handling from jhoekx, you might want to  
> try to pull again, but this looks like it.

Thanks Michael: tested & works.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Ajax](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Ajax](https://forum.ansible.com/u/Ajax)\
**Post date:** [July 18, 2012, 6:32pm UTC](https://forum.ansible.com/t/ansible-node-variables/11688/8 "2012-07-18T18:32:01Z")

</div>

Thank Michael and JP, works with latest version.

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [July 18, 2012, 6:49pm UTC](https://forum.ansible.com/t/ansible-node-variables/11688/9 "2012-07-18T18:49:03Z")

</div>

> Thank Michael and JP, works with latest version.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^^^^

I didn't fix it; I just complained. @jhoekx did the work. 🙂

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP
