Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ansible ad hoc debug module not print variable?

Tags:

ansible

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(...)?)

like image 664
Benjamin Berman Avatar asked Sep 03 '26 01:09

Benjamin Berman


1 Answers

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
}
like image 183
larsks Avatar answered Sep 04 '26 14:09

larsks