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

72 views
Skip to first unread message

Adrian Sevcenco

unread,
Dec 13, 2022, 6:31:37 PM12/13/22
to Ansible Project
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

Dick Visser

unread,
Dec 14, 2022, 1:05:47 AM12/14/22
to ansible...@googlegroups.com
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?


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/514517aa-4361-c76a-1410-8523c13f7174%40gmail.com.
--
Sent from Gmail Mobile

Dick Visser

unread,
Dec 14, 2022, 1:15:23 AM12/14/22
to ansible...@googlegroups.com
On Wed, 14 Dec 2022 at 00:31, Adrian Sevcenco <adria...@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 

ansible.utils.ipaddr('XX.XXX.XX.0/24')

Or am I missing something?

Thx!

Adrian Sevcenco

unread,
Dec 14, 2022, 2:52:36 AM12/14/22
to ansible...@googlegroups.com
On 14.12.2022 08:05, Dick Visser wrote:
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> <https://groups.google.com/d/msgid/ansible-project/514517aa-4361-c76a-1410-8523c13f7174%40gmail.com>.
>
> --
> Sent from Gmail Mobile
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAF8BbLa-YT2JFrSgJxZPz3-yzav0V4AUz9y2T-UCmnx8grN1-A%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAF8BbLa-YT2JFrSgJxZPz3-yzav0V4AUz9y2T-UCmnx8grN1-A%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Adrian Sevcenco

unread,
Dec 14, 2022, 3:07:36 AM12/14/22
to ansible...@googlegroups.com
On 14.12.2022 08:14, Dick Visser wrote:
>
>
> On Wed, 14 Dec 2022 at 00:31, Adrian Sevcenco <adria...@gmail.com <mailto:adria...@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?
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

Reply all
Reply to author
Forward
0 new messages