# I want to access a variable from the "ansible\_facts" and get a FAILED with undefined variable

**URL:** <https://forum.ansible.com/t/i-want-to-access-a-variable-from-the-ansible-facts-and-get-a-failed-with-undefined-variable/44378>\
**Category:** Get Help\
**Created:** [August 27, 2025, 11:34am UTC](https://forum.ansible.com/t/i-want-to-access-a-variable-from-the-ansible-facts-and-get-a-failed-with-undefined-variable/44378 "2025-08-27T11:34:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschm103](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@mschm103](https://forum.ansible.com/u/mschm103)\
**Post date:** [August 27, 2025, 11:34am UTC](https://forum.ansible.com/t/i-want-to-access-a-variable-from-the-ansible-facts-and-get-a-failed-with-undefined-variable/44378/1 "2025-08-27T11:34:55Z")

</div>

* * *

- name: Testen des lineinfile Modules  
hosts: d127test  
gather\_facts: yes  
become: yes

i become:

fatal: [d127test]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘address’. ‘list object’ has no attribute ‘address’\n\nThe error appears to be in ‘/home/mschm103/ansiblehome/ms\_test\_rubrik.yml’: line 11, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Filtern der IP-Adresse von en6\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![mschm103](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@mschm103](https://forum.ansible.com/u/mschm103)\
**Post date:** [August 27, 2025, 11:45am UTC](https://forum.ansible.com/t/i-want-to-access-a-variable-from-the-ansible-facts-and-get-a-failed-with-undefined-variable/44378/2 "2025-08-27T11:45:48Z")

</div>

How can I set up the playbook here without losing the formatting?

---

<div class="post-metadata">

**Author:** ![anon81884396](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anon81884396](https://forum.ansible.com/u/anon81884396)\
**Post date:** [August 27, 2025, 12:10pm UTC](https://forum.ansible.com/t/i-want-to-access-a-variable-from-the-ansible-facts-and-get-a-failed-with-undefined-variable/44378/3 "2025-08-27T12:10:48Z")

</div>

You didn’t ask a question in your original post, so I’m not sure what you’re looking for here. You are checking

`when: "'en6' in ansible_facts"`

But are failing on `ansible_facts['en6']['ipv4']['address']`. The error message says ansible\_facts[‘en6’][‘ipv4’] doesn’t contain “address”. Try using debug to print it out and see what it contains and reference accordingly.

For your second post, if you’re asking how to format code in a post, use triple backticks (```) to enclose the code like so:

```auto
hello
1
2
3

```

---

<div class="post-metadata">

**Author:** ![mschm103](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@mschm103](https://forum.ansible.com/u/mschm103)\
**Post date:** [August 27, 2025, 12:30pm UTC](https://forum.ansible.com/t/i-want-to-access-a-variable-from-the-ansible-facts-and-get-a-failed-with-undefined-variable/44378/4 "2025-08-27T12:30:08Z")

</div>

```auto

    - name: Filtern der IP-Adresse von en6
      ansible.builtin.debug:
        msg: "{{ ansible_facts['en6']['ipv4'] }}"
      when: "'en6' in ansible_facts"
      register: ip

```

Then i become:

```auto
    "msg": [
        {
            "address": "100.70.34.119",
            "broadcast": "100.70.63.255",
            "netmask": "255.255.224.0",
            "network": "100.70.32.0"
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![anon81884396](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anon81884396](https://forum.ansible.com/u/anon81884396)\
**Post date:** [August 27, 2025, 12:34pm UTC](https://forum.ansible.com/t/i-want-to-access-a-variable-from-the-ansible-facts-and-get-a-failed-with-undefined-variable/44378/5 "2025-08-27T12:34:31Z")

</div>

If I’m reading it right, it looks like `ansible_facts['en6']['ipv4']` is actually a list (or array) of dicts, so you’d need to iterate through each entry in a loop or do some kind of fancy json selection thing beyond my skill level (though sometimes AI tools are handy for this).

Something similar to

```auto
    - name: Show items in en6
      ansible.builtin.debug:
        msg: "{{ item }}"
      loop: "{{ ansible_facts['en6']['ipv4'] }}"

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 26, 2025, 12:35pm UTC](https://forum.ansible.com/t/i-want-to-access-a-variable-from-the-ansible-facts-and-get-a-failed-with-undefined-variable/44378/6 "2025-09-26T12:35:17Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
