Configure IP in Ubuntu20

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 :wink: ?

Thank in advance and best regards

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 :wink: ?

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:
     my_nics: "{{ mynics | default() + [{ ansible_facts[item].device: ansible_facts[item].active }] }}"
   when:
     - ansible_facts[item].type == 'ether'
   loop: "{{ ansible_interfaces }}"

- debug:
     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
        Racke

Ansible module "setup" collects this kind of info. See
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html

For example, the playbook below

  - hosts: localhost
    tasks:
      - debug:
          var: ansible_interfaces
      - debug:
          var: ansible_facts.eth0
      - debug:
          var: ansible_facts.wlan0
      - debug:
          msg: "{{ ansible_facts|dict2items|
                   json_query('[?value.active].key') }}"

gives

  ok: [localhost] => {
      "ansible_interfaces": [
          "lo",
          "eth0",
          "wlan0"
      ]
  }

  ok: [localhost] => { "ansible_facts.eth0": {
          "active": true,
          "device": "eth0",
          "features": {
              "esp_hw_offload": "off [fixed]",
          ...

  ok: [localhost] => {
      "ansible_facts.wlan0": {
          "active": false,
          "device": "wlan0",
          "features": {
              "esp_hw_offload": "off [fixed]",
          ...

  ok: [localhost] => {
      "msg": [
          "lo",
          "eth0"
      ]
  }