22

Ansible variables come from a variety of sources. It is for example possible to provide host_vars and group_vars by creating YAML files in a subfolder named host_vars and group_vars respectively of the folder containing the inventory file.

How can I list all of the variables Ansible would know about a group or host inside a playbook? Note: I tried ansible -m debug -e 'var=hostvars' host and ansible -m debug -e '- debug: var=hostvars' to no avail.

Hint: ansible <group|host> -m setup is not the correct answer as it does not include all the variables that come from other sources (it only contains { "ansible_facts" : { ... } }. In fact it does not even include variables provided by a dynamic inventory script (via _meta and so on).

Ansible version: 1.9.1.

20

ansible -m debug -a "var=hostvars[inventory_hostname]" seems to work. Valid variable sources (host_vars, group_vars, _meta in a dynamic inventory, etc.) are all taken into account.

With dynamic inventory script hosts.sh:

#!/bin/sh
if test "$1" = "--host"; then
        echo {}
else
        cat <<EOF
{
  "ungrouped": [ "x.example.com", "y.example.com" ],
  "group1": [ "a.example.com" ],
  "group2": [ "b.example.com" ],
  "groups": {
    "children": [ "group1", "group2" ],
    "vars": { "ansible_ssh_user": "user" }
  },
  "_meta": {
    "hostvars": {
      "a.example.com": { "ansible_ssh_host": "10.0.0.1" },
      "b.example.com": { "ansible_ssh_host": "10.0.0.2" }
    }
  }
}
EOF
fi

You can get:

$ chmod +x hosts.sh
$ ansible -i hosts.sh a.example.com -m debug -a "var=hostvars[inventory_hostname]"
a.example.com | success >> {
    "var": {
        "hostvars": {
            "ansible_ssh_host": "10.0.0.1", 
            "ansible_ssh_user": "user", 
            "group_names": [
                "group1", 
                "groups"
            ], 
            "groups": {
                "all": [
                    "x.example.com", 
                    "y.example.com", 
                    "a.example.com", 
                    "b.example.com"
                ], 
                "group1": [
                    "a.example.com"
                ], 
                "group2": [
                    "b.example.com"
                ], 
                "groups": [
                    "a.example.com", 
                    "b.example.com"
                ], 
                "ungrouped": [
                    "x.example.com", 
                    "y.example.com"
                ]
            }, 
            "inventory_hostname": "a.example.com", 
            "inventory_hostname_short": "a"
        }
    }
}
  • With ansible 2.0.2, this doesn't seem to work anymore. The output is localhost | SUCCESS => { "hostvars": "<ansible.vars.hostvars.HostVars object at 0x7f320943da10>" } – Zulakis Feb 25 '16 at 10:34
  • Suggested edit to use "var=hostvars[inventory_hostname]" on ansible > 2.0 – stuart-warren Mar 9 '16 at 13:37
  • For 1.9.4 it doesn't return the stuff returned by ansible my.hostname.example.com -m setup -i ../my/inventory/hosts.example -u root – akostadinov Mar 11 '16 at 15:07
  • 1
    This worked for me ansible host-name -m debug -a "var=[var_name]" -i inventory/testing/hosts – Montaro Sep 18 '17 at 12:09
2

FYI: This github project shows you how to list 90% of variables across all hosts. I find it more globally useful than single host commands. The README includes instructions for building a simple inventory report. It's even more valuable to run this at the end of a playbook to see all the Facts. To also debug Task behaviour use register:

2

Adding a small tip to the really good answer above, if you want to programmatically poke around you can

Use the existing answer for hostvars:

ansible -m debug myhost -a "var=hostvars[inventory_hostname].ansible_version"

But ansible_facts is empty because debug doesn't run the setup module. So you need to try something extra like jq after trimming the output to make it valid json.

ansible -m setup myhost | sed 's#.*SUCCESS =>##' | jq .ansible_facts.ansible_all_ipv4_addresses

I thought people might find this useful when investigating the giant wall of text that comes back in ansible facts when you just want one thing like jq .ansible_facts.ansible_devices.vda.size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.