# How to handle "inactive" network card

**URL:** <https://forum.ansible.com/t/how-to-handle-inactive-network-card/26999>\
**Category:** Ansible Project\
**Created:** [March 14, 2018, 5:44pm UTC](https://forum.ansible.com/t/how-to-handle-inactive-network-card/26999 "2018-03-14T17:44:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julien\_Deloubes](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@Julien\_Deloubes](https://forum.ansible.com/u/Julien_Deloubes)\
**Post date:** [March 14, 2018, 5:44pm UTC](https://forum.ansible.com/t/how-to-handle-inactive-network-card/26999/1 "2018-03-14T17:44:57Z")

</div>

Hello Ansible users,  
i have a nice playbook that make a convenient hash table for network card like that:  
{  
“192.168.0.1”: “eth0”,  
“192.168.0.2”: “eth1”  
}

Here is the playbook:

- set\_fact:  
ifaces: “{{ ifaces|default({}) | combine({hostvars[item][‘ansible\_%s’ | format(local\_iface)][‘ipv4’][‘address’]: local\_iface}) }}”  
with\_items:
- “{{ hostvars[item][‘ansible\_interfaces’] }}”  
loop\_control:  
loop\_var: local\_iface

All was working fine but i encounter an error when i launch it with a host that have a network card with no ip address.  
The playbook fails with the following error:

"FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘ipv4’”

Do you have any idea how i can handle this case with a special condition to ignore network card with no ipv4 address ?

Thanks!

---

<div class="post-metadata">

**Author:** ![Julien\_Deloubes](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@Julien\_Deloubes](https://forum.ansible.com/u/Julien_Deloubes)\
**Post date:** [March 16, 2018, 12:11pm UTC](https://forum.ansible.com/t/how-to-handle-inactive-network-card/26999/2 "2018-03-16T12:11:08Z")

</div>

I forgot to say that “item” refer to a list of hosts in a group.  
I tried with these special condition:

- set\_fact:  
**when: hostvars[item][‘ansible\_’~local\_iface][‘ipv4’] is defined**  
ifaces: “{{ ifaces|default({}) | combine({hostvars[item][‘ansible\_%s’ | format(local\_iface)][‘ipv4’][‘address’]: local\_iface}) }}”  
with\_items:
- “{{ hostvars[item][‘ansible\_interfaces’] }}”  
loop\_control:  
loop\_var: local\_iface

But with no more luck.  
Any ideas?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 18, 2018, 6:47am UTC](https://forum.ansible.com/t/how-to-handle-inactive-network-card/26999/3 "2018-03-18T06:47:42Z")

</div>

The when is indented to far in, is should be on the same level as set\_fact.

---

<div class="post-metadata">

**Author:** ![Julien\_Deloubes](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@Julien\_Deloubes](https://forum.ansible.com/u/Julien_Deloubes)\
**Post date:** [March 18, 2018, 9:20pm UTC](https://forum.ansible.com/t/how-to-handle-inactive-network-card/26999/4 "2018-03-18T21:20:43Z")

</div>

Yeah sure, sorry wrong copy/paste i haven’t syntax problem on my original playbook.  
Anyway i found the solution which is:

- set\_fact:  
ifaces: “{{ ifaces|default({}) | combine({hostvars[item][‘ansible\_%s’ | format(local\_iface)][‘ipv4’][‘address’]: local\_iface}) }}”  
when: hostvars[item][‘ansible\_’~local\_iface][‘ipv4’] is defined  
with\_items:
- “{{ hostvars[item][‘ansible\_interfaces’] }}”  
loop\_control:  
loop\_var: local\_iface
