# Configure IP in Ubuntu20

**URL:** <https://forum.ansible.com/t/configure-ip-in-ubuntu20/34238>\
**Category:** Ansible Project\
**Created:** [November 25, 2020, 2:32pm UTC](https://forum.ansible.com/t/configure-ip-in-ubuntu20/34238 "2020-11-25T14:32:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [November 25, 2020, 2:32pm UTC](https://forum.ansible.com/t/configure-ip-in-ubuntu20/34238/1 "2020-11-25T14:32:16Z")

</div>

Hi All,

I’m tried to check up which interfaces isn’t configured in Ubuntu20 to configure it.  
So, first I check which interfaces are on the system. Then it will check if this device is UP or not. I want to retrieve which interface isn’t UP and put it a value in it: like :

ens160: 0  
ens162: 1

For that in the next role later, I can configure the interface, when value=1.

- name: check which interfaces exist  
shell: find /sys/class/net -type l -not -lname ‘_virtual_’ -printf ‘%f\n’  
register: ifconfig\_result

- name: check which interface is active  
shell: ifconfig -a “{{ item }}”|head -1|grep -v UP || /bin/true  
loop: “{{ ifconfig\_result.stdout\_lines }}”  
register: active

- name: debug  
debug:  
msg: “{{ item }}”  
loop: “{{ active.results }}”

I’ve seen with debug, two things: first one, both results are ok because /bin/true and secondly, the item is not the key.  
For this reason I don’t know if it is possible in this case to retrieve this info.  
Perhaps, there’s another easiest solution???

As someone an idea?  
Could someone enlighten me 😉 ?

Thank in advance and best regards

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 25, 2020, 3:28pm UTC](https://forum.ansible.com/t/configure-ip-in-ubuntu20/34238/2 "2020-11-25T15:28:19Z")

</div>

> Hi All,
> 
> I'm tried to check up which interfaces isn't configured in Ubuntu20 to configure it.  
> So, first I check which interfaces are on the system. Then it will check if this device is UP or not. I want to retrieve  
> which interface isn't UP and put it a value in it: like :
> 
> ens160: 0  
> ens162: 1
> 
> For that in the next role later, I can configure the interface, when value=1.
> 
> - name: check which interfaces exist  
> shell: find /sys/class/net -type l -not -lname '\*virtual\*' -printf '%f\n'  
> register: ifconfig\_result
> 
> - name: check which interface is active  
> shell: ifconfig -a "{{ item }}"|head -1|grep -v UP || /bin/true  
> loop: "{{ ifconfig\_result.stdout\_lines }}"  
> register: active
> 
> - name: debug  
> debug:  
> msg: "{{ item }}"  
> loop: "{{ active.results }}"
> 
> I've seen with debug, two things: first one, both results are ok because /bin/true and secondly, the item is not the key.  
> For this reason I don't know if it is possible in this case to retrieve this info.  
> Perhaps, there's another easiest solution???  
> As someone an idea?  
> Could someone enlighten me 😉 ?
> 
> Thank in advance and best regards

The information about the network interfaces is already in the Ansible facts, so you can do the following:

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_nics: "{{ mynics | default() + [{ ansible\_facts[item].device: ansible\_facts[item].active }] }}"  
&nbsp;&nbsp;&nbsp;when:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ansible\_facts[item].type == 'ether'  
&nbsp;&nbsp;&nbsp;loop: "{{ ansible\_interfaces }}"

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_nics

The list of interfaces is in the "ansible\_interfaces" variable and each interface is itself in the facts e.g.  
"ansible\_eth0" or "ansible\_facts[eth0]".

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 25, 2020, 3:32pm UTC](https://forum.ansible.com/t/configure-ip-in-ubuntu20/34238/3 "2020-11-25T15:32:31Z")

</div>

Ansible module "setup" collects this kind of info. See  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html)

For example, the playbook below

&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_interfaces  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_facts.eth0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_facts.wlan0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts|dict2items|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query('[?value.active].key') }}"

gives

&nbsp;&nbsp;ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_interfaces": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"lo",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"eth0",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"wlan0"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;}

&nbsp;&nbsp;ok: [localhost] =\> { "ansible\_facts.eth0": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"active": true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"device": "eth0",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"features": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"esp\_hw\_offload": "off [fixed]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

&nbsp;&nbsp;ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_facts.wlan0": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"active": false,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"device": "wlan0",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"features": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"esp\_hw\_offload": "off [fixed]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

&nbsp;&nbsp;ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"lo",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"eth0"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;}
