verb
verb

Reputation: 89

Variable is undefined when running Ansible 'debug' ad-hoc

Could you explain why following behaviour happens. When I try to print remote Ansible IP with following playbook everything works as expected:

---
- hosts: centos1
  tasks:
    - name: Print ip address
      debug:
        msg: "ip: {{ansible_all_ipv4_addresses[0]}}"

when I try ad-hoc command it doesn't work:

ansible -i hosts centos1 -m debug -a 'msg={{ansible_all_ipv4_addresses[0]}}'

Here is the ad-hoc error:

centos1 | FAILED! => { "msg": "The task includes an option with an undefined variable. The error was: 'ansible_all_ipv4_addresses' is undefined. 'ansible_all_ipv4_addresses' is undefined" }

I don't find any difference in both approaches that is why I was expecting both to work and print the remote IP address.

Upvotes: 3

Views: 865

Answers (2)

Carl Walsh
Carl Walsh

Reputation: 7009

You can run ad-hoc commands that use gathered facts if you set up an ansible Cache plugin first. Notice the first debug fails, but after running setup once, now debug starts working.

$ export ANSIBLE_CACHE_PLUGIN=jsonfile
$ export ANSIBLE_CACHE_PLUGIN_CONNECTION=/tmp/ansible-cache
$ mkdir /tmp/ansible-cache

$ ansible myMachine -m debug -a 'msg={{ansible_all_ipv4_addresses[0]}}'
myMachine | FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'ansible_all_ipv4_addresses' is undefined. 'ansible_all_ipv4_addresses' is undefined"
}

$ ansible myMachine -m setup
myMachine | SUCCESS => {
    "ansible_facts": {
...
}

$ ansible myMachine -m debug -a 'msg={{ansible_all_ipv4_addresses[0]}}'
myMachine | SUCCESS => {
    "msg": "10.50.1.1"
}

Upvotes: 1

U880D
U880D

Reputation: 12144

I don't find any difference in both approaches that is why I was expecting both to work and print the remote IP address.

This is because no facts were gathered. Whereby via ansible-playbook and depending on the configuration Ansible facts become gathered automatically, via ansible only and ad-hoc command not.

To do so you would need to execute the setup module instead. See Introduction to ad hoc commands - Gathering facts.

Further Q&A

Please take note of the variable names according


Could you please give some example on How to output "Your IP address is "{{ ansible_all_ipv4_addresses[0] }}"? using ad-hoc approach with setup module?

Example

ansible test.example.com -m setup -a 'filter=ansible_all_ipv4_addresses'
test.example.com | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.0.2.1"
        ]
    },
    "changed": false
}

or

ansible test.example.com -m setup -a 'filter=ansible_default_ipv4'
test.example.com | SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "192.0.2.1",
            "alias": "eth0",
            "broadcast": "192.0.2.255",
            "gateway": "192.0.2.0",
            "interface": "eth0",
            "macaddress": "00:00:5e:12:34:56",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "192.0.2.0",
            "type": "ether"
        }
    },
    "changed": false
}

It is also recommend to have a look into the full output without the filter argument to get familiar with the result set and data structure.

Documentation

Upvotes: 2

Related Questions