4

I'm trying to debug my ansible setup by running

ansible -m debug -a 'var=ansible_distribution' all

but I'm getting

my_ansible_host0 | SUCCESS => {
    "ansible_distribution": "VARIABLE IS NOT DEFINED!"
}

It seems I can use {{ansible_distribution}} in my jinja templates, though. Why is this? (Is this, for example, something to do with the distinction between facts and variables that I haven't been able to figure out yet?) And how can I change my command to get it to print out the value of ansible_distribution? (Do I need to do something with lookup(...)?)

Benjamin Berman
  • 521
  • 4
  • 15

1 Answers1

9

The ansible_distribution fact is created implicitly by the setup module when you run a play. You may have noticed this when running a playbook:

TASK [Gathering Facts] ****************************************************************

This is Ansible running the setup module to get information about a remote host. When you're running ad-hoc commands, there is no "gathering facts" step, so these variables aren't available.

If you want to see the value of ansible_distribution and other variables, you can manually run the setup module:

ansible -m setup all

You can limit collected facts using parameters to the setup module. For example:

$ ansible localhost -m setup -a 'filter=ansible_dist*'

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "Fedora",
        "ansible_distribution_file_parsed": true,
        "ansible_distribution_file_path": "/etc/redhat-release",
        "ansible_distribution_file_variety": "RedHat",
        "ansible_distribution_major_version": "31",
        "ansible_distribution_release": "",
        "ansible_distribution_version": "31"
    },
    "changed": false
}
larsks
  • 277,717
  • 41
  • 399
  • 399