# Problem by setting up Network Interface (Newbie)

**URL:** <https://forum.ansible.com/t/problem-by-setting-up-network-interface-newbie/29158>\
**Category:** Ansible Project\
**Created:** [November 30, 2018, 7:50am UTC](https://forum.ansible.com/t/problem-by-setting-up-network-interface-newbie/29158 "2018-11-30T07:50:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phillip](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@Phillip](https://forum.ansible.com/u/Phillip)\
**Post date:** [November 30, 2018, 7:50am UTC](https://forum.ansible.com/t/problem-by-setting-up-network-interface-newbie/29158/1 "2018-11-30T07:50:03Z")

</div>

Hi All,

I’m new to Ansible and take over an existing Ansible setup from my co-worker.  
Now the playbook worked on his configuration but now I made a modification with a new host and I receive an error message which doesn’t makes a whole lot of sense for me.

Ansible Version: 2.4.2.0-2.el7  
Ansible OS: CentOS 7.5.1804  
Receiving Server: CentOS 7.4.1708

ok: [server1.infra.sys] =\> (item={u’ipaddr’: u’1.1.1.2’, u’netmask’: u’255.255.255.0’, u’bootproto’: u’none’, u’device’: u’ens192’, u’type’: u’Ethernet’, u’gateway’: u’1.1.1.250’}) =\> {“changed”: false, “checksum”: “fdecaa26f260854783e24a79c166604488713c90”, “gid”: 0, “group”: “root”, “item”: {“bootproto”: “none”, “device”: “ens192”, “gateway”: “1.1.1.250”, “ipaddr”: “1.1.1.2”, “netmask”: “255.255.255.0”, “type”: “Ethernet”}, “mode”: “0644”, “owner”: “root”, “path”: “/etc/sysconfig/network-scripts/ifcfg-ens192”, “secontext”: “system\_u:object\_r:net\_conf\_t:s0”, “size”: 229, “state”: “file”, “uid”: 0}

fatal: [server1.infra.sys]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘device’\n\nThe error appears to have been in ‘/opt/ansible/playbooks/roles/basic/tasks/main.yml’: line 36, 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: Update primary interface IP information\n ^ here\n\nexception type: \<class ‘ansible.errors.AnsibleUndefinedVariable’\>\nexception: ‘dict object’ has no attribute ‘device’”}

The NIC got configured on the Server. the ifcfg-ens192 file looks like this now:

> cat /etc/sysconfig/network-scripts/ifcfg-ens192

> > #######
> 
> > # Managed via Ansible
> 
> > #######
> 
> > NAME=ens192
> 
> > TYPE=Ethernet
> 
> > DEVICE=ens192
> 
> > BOOTPROTO=none
> 
> > ONBOOT=yes
> 
> > NM\_CONTROLLED=no
> 
> > DEFROUTE=yes
> 
> > IPV4\_FAILURE\_FATAL=no
> 
> > IPV6INIT=no
> 
> > IPADDR=1.1.1.2
> 
> > PREFIX=255.255.255.0
> 
> > GATEWAY=1.1.1.250

This is what I have written in to the host file in the inventory directory/./host\_vars/server1.infra.sys:

> # HOST INTERFACES DEFINITIONS
> 
> interfaces:
> 
> - device: ens192
> 
> type: Ethernet
> 
> bootproto: none
> 
> ipaddr: 1.1.1.2
> 
> netmask: 255.255.255.0
> 
> gateway: 1.1.1.250

As seen, there is device configured and in the error msg from Ansible it isn’t happy because the variable ‘device’ isn’t set.

This is a part of /opt/ansible/playbooks/roles/basic/tasks/main.yml

> - name: Update primary interface IP information
> 
> template:
> 
> src: roles/basic/templates/ifcfg.j2
> 
> dest: “/etc/sysconfig/network-scripts/ifcfg-{{item.device}}”
> 
> owner: root
> 
> group: root
> 
> mode: 0644
> 
> with\_items: “{{ interfaces }}”
> 
> when: interfaces is defined
> 
> notify: restart network

So why should I get an error from Ansible when the changes have been made successfully?

I’m already very thankful for the time and help with my issue.

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [November 30, 2018, 7:54am UTC](https://forum.ansible.com/t/problem-by-setting-up-network-interface-newbie/29158/2 "2018-11-30T07:54:06Z")

</div>

When I'm debugging Ansible Playbooks and I don't know if I can access a variable I usually have a debug block.

In your case this might look something like:

---

<div class="post-metadata">

**Author:** ![Phillip](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@Phillip](https://forum.ansible.com/u/Phillip)\
**Post date:** [November 30, 2018, 8:01am UTC](https://forum.ansible.com/t/problem-by-setting-up-network-interface-newbie/29158/3 "2018-11-30T08:01:36Z")

</div>

Hi Uwe,  
Thank you for your help.

When I insert the - debug then I get this feedback of it:

TASK [basic : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [server1.infra.sys]: FAILED! =\> {“msg”: “Unexpected failure in finding the lookup named ‘{{ interfaces }}’ in the available lookup plugins”}

---

<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:** [November 30, 2018, 8:13am UTC](https://forum.ansible.com/t/problem-by-setting-up-network-interface-newbie/29158/4 "2018-11-30T08:13:06Z")

</div>

> ok: [server1.infra.sys] =\> (item={u'ipaddr': u'1.1.1.2', u'netmask':  
> u'255.255.255.0', u'bootproto': u'none', u'device': u'ens192', u'type':  
> u'Ethernet', u'gateway': u'1.1.1.250'}) =\> {"changed": false, "checksum":  
> "fdecaa26f260854783e24a79c166604488713c90", "gid": 0, "group": "root",  
> "item": {"bootproto": "none", "device": "ens192", "gateway": "1.1.1.250",  
> "ipaddr": "1.1.1.2", "netmask": "255.255.255.0", "type": "Ethernet"},  
> "mode": "0644", "owner": "root", "path":  
> "/etc/sysconfig/network-scripts/ifcfg-ens192", "secontext":  
> "system\_u:object\_r:net\_conf\_t:s0", "size": 229, "state": "file", "uid": 0}
> 
> fatal: [server1.infra.sys]: FAILED! =\> {"msg": "The task includes an option  
> with an undefined variable. The error was: 'dict object' has no attribute  
> 'device'\n\nThe error appears to have been in  
> '/opt/ansible/playbooks/roles/basic/tasks/main.yml': line 36, 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: Update primary interface IP  
> information\n ^ here\n\nexception type: \<class  
> 'ansible.errors.AnsibleUndefinedVariable'\>\nexception: 'dict object' has no  
> attribute 'device'"}

So you are running this task twice, so you with\_items aka interfaces variable contain at least to list elements.

> This is what I have written in to the host file in the inventory  
> directory/./host\_vars/server1.infra.sys:
> 
> # HOST INTERFACES DEFINITIONS  
> interfaces:  
> - device: ens192  
> &nbsp;&nbsp;&nbsp;type: Ethernet  
> &nbsp;&nbsp;&nbsp;bootproto: none  
> &nbsp;&nbsp;&nbsp;ipaddr: 1.1.1.2  
> &nbsp;&nbsp;&nbsp;netmask: 255.255.255.0  
> &nbsp;&nbsp;&nbsp;gateway: 1.1.1.250

My suspicion is that this is not the whole content of the file, because your output indicate that you have at least one more element in this list.

> As seen, there is device configured and in the error msg from Ansible it  
> isn't happy because the variable 'device' isn't set.
> 
> This is a part of /opt/ansible/playbooks/roles/basic/tasks/main.yml
> 
> - name: Update primary interface IP information  
> &nbsp;&nbsp;template:  
> &nbsp;&nbsp;&nbsp;src: roles/basic/templates/ifcfg.j2  
> &nbsp;&nbsp;&nbsp;dest: "/etc/sysconfig/network-scripts/ifcfg-{{item.device}}"  
> &nbsp;&nbsp;&nbsp;owner: root  
> &nbsp;&nbsp;&nbsp;group: root  
> &nbsp;&nbsp;&nbsp;mode: 0644  
> &nbsp;&nbsp;with\_items: "{{ interfaces }}"  
> &nbsp;&nbsp;when: interfaces is defined  
> &nbsp;&nbsp;notify: restart network
> 
> So why should I get an error from Ansible when the changes have been made  
> successfully?

The first element in the list interfaces goes thru OK but not the second one.

If you before the task that fails add this

&nbsp;&nbsp;&nbsp;- debug: var=interfaces

you can verify that you variable actually contain more than one list element.

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [November 30, 2018, 8:16am UTC](https://forum.ansible.com/t/problem-by-setting-up-network-interface-newbie/29158/5 "2018-11-30T08:16:23Z")

</div>

Depending on the Ansible version it might be necessary to change

loop: '{{ interfaces }}'

to

with\_items:  
&nbsp;&nbsp;- '{{ interfaces }}'

Yuo migth as well change the whole debug output as suggested by Kai Stian Olstad .

---

<div class="post-metadata">

**Author:** ![Phillip](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@Phillip](https://forum.ansible.com/u/Phillip)\
**Post date:** [November 30, 2018, 9:08am UTC](https://forum.ansible.com/t/problem-by-setting-up-network-interface-newbie/29158/6 "2018-11-30T09:08:45Z")

</div>

Hi Kai

Ok, with the new modification in main.yml with:

> - debug: var=interfaces
> 
> - meta: end\_play

does it works as designed.  
Here the output of the debug:

> ok: [server1.infra.sys] =\> {
> 
> “interfaces”: [
> 
> {
> 
> “bootproto”: “none”,
> 
> “device”: “ens192”,
> 
> “gateway”: “1.1.1.250”,
> 
> “ipaddr”: “1.1.1.2”,
> 
> “netmask”: “255.255.255.0”,
> 
> “type”: “Ethernet”
> 
> },
> 
> {
> 
> “aliases”: “ansible”,
> 
> “fqdn”: “ansible.infra.sys”,
> 
> “host”: “1.1.1.3”
> 
> }
> 
> ]
> 
> }

This is the complete configuration of the interfaces and additional services:

> fqdn: server1.infra.sys

> # HOST INTERFACES DEFINITIONS
> 
> interfaces:
> 
> - device: ens192
> 
> type: Ethernet
> 
> bootproto: none
> 
> ipaddr: 1.1.1.2
> 
> netmask: 255.255.255.0
> 
> gateway: 1.1.1.250
> 
> # /etc/hosts ENTRIES
> 
> - host: 1.1.1.3
> 
> fqdn: ansible.infra.sys
> 
> aliases: “ansible”
> 
> ntp\_servers:
> 
> - server: 1.1.1.5
> 
> - server: 1.1.1.6
> 
> ntp\_interface: ens192

So when I understand the debug correct, device is set so it shouldn’t be any problem. Or do I need so specify anything else with device on an other part of the configuration?

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [November 30, 2018, 9:12am UTC](https://forum.ansible.com/t/problem-by-setting-up-network-interface-newbie/29158/7 "2018-11-30T09:12:02Z")

</div>

I think you don't understand the structure of "interfaces"

> Here the output of the debug:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;ok: [server1.infra.sys] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp; "interfaces": [  
> &nbsp;&nbsp;&nbsp;&nbsp; {  
> &nbsp;&nbsp;&nbsp;&nbsp; "bootproto": "none",  
> &nbsp;&nbsp;&nbsp;&nbsp; "device": "ens192",  
> &nbsp;&nbsp;&nbsp;&nbsp; "gateway": "1.1.1.250",  
> &nbsp;&nbsp;&nbsp;&nbsp; "ipaddr": "1.1.1.2",  
> &nbsp;&nbsp;&nbsp;&nbsp; "netmask": "255.255.255.0",  
> &nbsp;&nbsp;&nbsp;&nbsp; "type": "Ethernet"  
> &nbsp;&nbsp;&nbsp;&nbsp; },  
> &nbsp;&nbsp;&nbsp;&nbsp; {  
> &nbsp;&nbsp;&nbsp;&nbsp; "aliases": "ansible",  
> &nbsp;&nbsp;&nbsp;&nbsp; "fqdn": "ansible.infra.sys",  
> &nbsp;&nbsp;&nbsp;&nbsp; "host": "1.1.1.3"  
> &nbsp;&nbsp;&nbsp;&nbsp; }  
> &nbsp;&nbsp;&nbsp;&nbsp; ]  
> &nbsp;&nbsp;&nbsp;&nbsp;}

This tells us that "interfaces" is a list with two elements. Both are dictionaries but they have differing keys. As such you  
cannot itterate over the elements of "interfaces" because in each iteration you are trying to access the key "device" for the  
current element.

The second element has no such key. This is why your play fails.

---

<div class="post-metadata">

**Author:** ![Phillip](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@Phillip](https://forum.ansible.com/u/Phillip)\
**Post date:** [November 30, 2018, 9:22am UTC](https://forum.ansible.com/t/problem-by-setting-up-network-interface-newbie/29158/8 "2018-11-30T09:22:24Z")

</div>

That is correct. At the moment I do not have a wide understanding of Ansible because i got thrown in to the cold water and got told “just do it”. So I’m a little struggling.

Ok ok ok, I found the issue with your help and the exploration how Ansible is build up.  
I missed out a line in the host\_vars file for the server. The red marked line missed so Ansible thought, that the - host part should attache to interfaces. But it is a different function which should write a static entry to the /etc/hosts file.

So after the correction (marked in red) the error have been resolved.

> # HOST INTERFACES DEFINITIONS
> 
> interfaces:
> 
> - device: ens192
> 
> type: Ethernet
> 
> bootproto: none
> 
> ipaddr: 1.1.1.2
> 
> netmask: 255.255.255.0
> 
> gateway: 1.1.1.250
> 
> # /etc/hosts ENTRIES
> 
> etc\_hosts:
> 
> - host: 1.1.1.250
> 
> fqdn: ansible.infra.sys
> 
> aliases: “ansible”

Thank you a lot for your time and patience with me as a newbie on Ansible. 🙂
