I’m attempting to loop over numerous network interfaces and send them to our NetBox instance. Unfortunately, due to the disparate types of servers and network interfaces that are provided, I would like to be able to use wildcards to loop over all interfaces of certain types. So far I have attempted the following:
Unsurprisingly, ansible isn’t happy when I tried to do so:
TASK [Write interfaces and addresses] ********************************************************************************************
fatal: [<hostname>]: FAILED! => {"msg": "'dict object' has no attribute 'ansible_eth*'. 'dict object' has no attribute 'ansible_eth*'"}
I’m looking for guidance on how to best iterate over the interfaces. Any advice is greatly appreciated. If it’s relevant, ansible version info is below:
I’m attempting to loop over numerous network interfaces and send them to our NetBox instance. Unfortunately, due to the disparate types of servers and network interfaces that are provided, I would like to be able to use wildcards to loop over all interfaces of certain types. So far I have attempted the following:
Unsurprisingly, ansible isn’t happy when I tried to do so:
TASK [Write interfaces and addresses] ********************************************************************************************
fatal: [<hostname>]: FAILED! => {"msg": "'dict object' has no attribute 'ansible_eth*'. 'dict object' has no attribute 'ansible_eth*'"}
I’m looking for guidance on how to best iterate over the interfaces. Any advice is greatly appreciated.
As a best practice you first filter facts using set_fact module using filters and iterate over the that variable or list of facts. loop matching the exact attribute name from facts.
ansible.builtin.set_fact:
my_loop_facts: “{{ ansible_facts[‘interfaces’] | select(‘search’, ‘^eth|^eno|^enp24s0f’) | list }}”
and then iterate over my_loop_facts variable.
loop:
my_loop_facts
I was able to get it working for the lookup based on the replies, but I’m having trouble referencing the variable from within ansible_facts. I would like to do something like this:
when:
- ansible_facts[\"{{ item }}\"][active] is true
but I’m not sure of what the correct syntax would be for referencing a variable from within the ansible_facts variable. Would anybody have a recommendation?