ansible node variables

It is -- it's pretty simple.

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

{
    ansible_facts: {
         'lein' : "path_to_lein_goes_here",
    }
}

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)

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”: “”}

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

Also can you run with --verbose?

– Michael

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.

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

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.

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.

        -JP

Thank Michael and JP, works with latest version.

Thank Michael and JP, works with latest version.

                ^^^^^^

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

        -JP