2

The task includes an option with an undefined variable. The error was: ansible_all_ipv4_addresses is undefined.

Why would I be getting this error, if I am connecting over ipv4? I'm trying to dump this like,

"{{ ansible_all_ipv4_addresses[0] }}"

And I can verify that it is valid,

$ ansible -u centos -m setup 10.1.38.15 | grep ansible_all_ipv4_addresses -A2 -B1
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.16.0.13"
        ],

But then very similar to the above,

$ ansible -u centos -m debug -a "msg='{{ansible_all_ipv4_addresses}}'" 10.1.38.15
10.1.38.15 | FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'ansible_all_ipv4_addresses' is undefined"
}
1
  • Have you tried using "{{ ansible_host }}"?
    – Panki
    Jan 10, 2020 at 8:25

2 Answers 2

3

For me the problem was that my playbook had

gather_facts: false

Set at the top of my playbook. As to why the use of facts does not work with the debug module, for that see this question

1

For me it was that Ubuntu in docker didn't have iproute2 package by default.

I see you are using CentOS, but it was the first link in Google, so i thought it's worth to mention this thread, where i actually found the answer.

Here is a block i added to my playbook, in case someone find it usful:

---
- name: Install iproute2 on ubuntu to gather facts properly
  block:
    - name: install iproute
      apt:
        name: iproute2
        state: present
    - name: Recollect facts
      ansible.builtin.setup:
        gather_subset:
          - all
    - debug: msg="{{ hostvars[inventory_hostname]['ansible_all_ipv4_addresses'] }}"
  when: ansible_distribution == "Ubuntu"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .