key from ansible_facts seen by debug but not by the actual task

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:
     msg: |
       ansible_all_ipv4_addresses: {{ ansible_all_ipv4_addresses }}
       _network_reduced: "{{ ansible_all_ipv4_addresses | ansible.utils.reduce_on_network('XX.XXX.XX.0/24') }}"
       _result_lenght: "{{ ansible_all_ipv4_addresses | ansible.utils.reduce_on_network('XX.XXX.XX.0/24') | length }}"
       _result_value: "{{ ( ansible_all_ipv4_addresses | ansible.utils.reduce_on_network('XX.XXX.XX.0/24') | length | int ) > 0 }}"

- name: Chrony configuration 85
   ansible.builtin.copy:
     src: "{{ playbook_dir }}/../additions/chrony/chrony.conf.client_pub85"
     dest: /etc/chrony.conf
     mode: 0644
     owner: root
     group: root
     backup: true
     force: true
   register: chrony_is_configured
   when:
     - ( ( 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

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?

On Wed, 14 Dec 2022 at 00:31, Adrian Sevcenco <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, 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!

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

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

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