# key from ansible\_facts seen by debug but not by the actual task

**URL:** <https://forum.ansible.com/t/key-from-ansible-facts-seen-by-debug-but-not-by-the-actual-task/37464>\
**Category:** Ansible Project\
**Created:** [December 13, 2022, 11:31pm UTC](https://forum.ansible.com/t/key-from-ansible-facts-seen-by-debug-but-not-by-the-actual-task/37464 "2022-12-13T23:31:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adriansev](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/adriansev/32/4866_2.png) [@adriansev](https://forum.ansible.com/u/adriansev)\
**Post date:** [December 13, 2022, 11:31pm UTC](https://forum.ansible.com/t/key-from-ansible-facts-seen-by-debug-but-not-by-the-actual-task/37464/1 "2022-12-13T23:31:16Z")

</div>

Hi! So, while i do use conditions like: ansible\_facts['distribution'] == 'CentOS'  
now i'm trying to use ansible\_all\_ipv4\_addresses

i have the following code:

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_all\_ipv4\_addresses: {{ ansible\_all\_ipv4\_addresses }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_network\_reduced: "{{ ansible\_all\_ipv4\_addresses | ansible.utils.reduce\_on\_network('XX.XXX.XX.0/24') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_result\_lenght: "{{ ansible\_all\_ipv4\_addresses | ansible.utils.reduce\_on\_network('XX.XXX.XX.0/24') | length }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_result\_value: "{{ ( ansible\_all\_ipv4\_addresses | ansible.utils.reduce\_on\_network('XX.XXX.XX.0/24') | length | int ) \> 0 }}"

- name: Chrony configuration 85  
&nbsp;&nbsp;&nbsp;ansible.builtin.copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ playbook\_dir }}/../additions/chrony/chrony.conf.client\_pub85"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/chrony.conf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0644  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: root  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: root  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;force: true  
&nbsp;&nbsp;&nbsp;register: chrony\_is\_configured  
&nbsp;&nbsp;&nbsp;when:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ( ( ansible\_facts['ansible\_all\_ipv4\_addresses'] | ansible.utils.reduce\_on\_network( 'XX.XXX.XX.0/24' ) | length | int ) \> 0 )

the problem is that the debug see the ansible\_all\_ipv4\_addresses while the when condition does not  
i get:

Dec 14 2022 01:18:00 - chrony\_cfg.yml - - debug - OK - {"msg": "ansible\_all\_ipv4\_addresses: ['XX.XXX.XX.21', '172.18.0.21']\n\_network\_reduced: \"['XX.XXX.XX.21']\"\n\_result\_lenght: \"1\"\n\_result\_value: \"True\"\n", "\_ansible\_verbose\_always": true, "\_ansible\_no\_log": null, "changed": false}

Dec 14 2022 01:18:00 - chrony\_cfg.yml - Chrony configuration 85 - ansible.builtin.copy - FAILED - {"msg": "The conditional check '( ( ansible\_facts['ansible\_all\_ipv4\_addresses'] | ansible.utils.reduce\_on\_network( 'XX.XXX.XX.0/24' )

> length | int ) \> 0 )' failed. The error was: error while evaluating conditional (( (

ansible\_facts['ansible\_all\_ipv4\_addresses'] | ansible.utils.reduce\_on\_network( 'XX.XXX.XX.0/24' ) | length | int ) \> 0 )): 'dict object' has no attribute 'ansible\_all\_ipv4\_addresses'. 'dict object' has no attribute 'ansible\_all\_ipv4\_addresses'\n\nThe error appears to be in '/home/adrian/ansible/playbooks/tasks/pkg\_chrony\_task.yml': line 15, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Chrony configuration 85\n ^ here\n", "\_ansible\_no\_log": false}

so, beside that i can check in my redis backend that 'ansible\_all\_ipv4\_addresses' is present, the fact is that  
the debug task works ..

So, does anyone have any idea what is going on?  
Thanks a lot!!  
Adrian

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [December 14, 2022, 6:05am UTC](https://forum.ansible.com/t/key-from-ansible-facts-seen-by-debug-but-not-by-the-actual-task/37464/2 "2022-12-14T06:05:19Z")

</div>

It’s the other way around from what you state in the subject.  
Your debug task using the variable ansible\_all\_ipv4\_addresses, which works.

But then when of your copy task uses ansible\_facts[‘ansible\_all\_ipv4\_addresses’], which gives the error.  
Try using just ansible\_all\_ipv4\_addresses in the when clause?

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [December 14, 2022, 6:14am UTC](https://forum.ansible.com/t/key-from-ansible-facts-seen-by-debug-but-not-by-the-actual-task/37464/3 "2022-12-14T06:14:56Z")

</div>

On Wed, 14 Dec 2022 at 00:31, Adrian Sevcenco \<[adrian.sev@gmail.com](mailto:adrian.sev@gmail.com)\>

> \_network\_reduced: “{{ ansible\_all\_ipv4\_addresses | ansible.utils.reduce\_on\_network(‘XX.XXX.XX.0/24’) }}”

Something unrelated, but I had not heard of this filter. Looking at the docs on  
[https://docs.ansible.com/ansible/latest/collections/ansible/utils/reduce\_on\_network\_filter.html](https://docs.ansible.com/ansible/latest/collections/ansible/utils/reduce_on_network_filter.html), it seems to me that it is doing the same thing as:

ansible.utils.ipaddr(‘XX.XXX.XX.0/24’)

Or am I missing something?

Thx!

---

<div class="post-metadata">

**Author:** ![adriansev](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/adriansev/32/4866_2.png) [@adriansev](https://forum.ansible.com/u/adriansev)\
**Post date:** [December 14, 2022, 7:52am UTC](https://forum.ansible.com/t/key-from-ansible-facts-seen-by-debug-but-not-by-the-actual-task/37464/4 "2022-12-14T07:52:19Z")

</div>

> It's the other way around from what you state in the subject.  
> Your debug task using the variable ansible\_all\_ipv4\_addresses, which works.
> 
> But then when of your copy task uses ansible\_facts['ansible\_all\_ipv4\_addresses'], which gives the error.  
> Try using just ansible\_all\_ipv4\_addresses in the when clause?

THANKS a lot!!! That was it!!!  
i was so hunged up on using the ansible\_facts like i use for OS conditions that i did not even considered  
despite the evidence show by debug module (where i considered that to be an artefact of jinja usage)

Once again many many thanks!!  
Adrian

---

<div class="post-metadata">

**Author:** ![adriansev](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/adriansev/32/4866_2.png) [@adriansev](https://forum.ansible.com/u/adriansev)\
**Post date:** [December 14, 2022, 8:07am UTC](https://forum.ansible.com/t/key-from-ansible-facts-seen-by-debug-but-not-by-the-actual-task/37464/5 "2022-12-14T08:07:12Z")

</div>

yeah, ipaddr just validate the input to being a valid ip  
reduce\_on network take a list of ips, and returns a list of ips that matched the given network, see

[https://docs.ansible.com/ansible/latest/collections/ansible/utils/reduce\_on\_network\_filter.html#ansible-collections-ansible-utils-reduce-on-network-filter](https://docs.ansible.com/ansible/latest/collections/ansible/utils/reduce_on_network_filter.html#ansible-collections-ansible-utils-reduce-on-network-filter)

the purpose was to copy a specific to network configuration file depending on the host ip.  
Ideally i should customize the name of the file depending on this (and not the task)  
but i do not know yet to write jinja "if else return" type of code

Thanks!  
Adrian
