Configure IP in Ubuntu20

20 views
Skip to first unread message

Hiero-nymo

unread,
Nov 25, 2020, 9:32:16 AM11/25/20
to Ansible Project
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

Stefan Hornburg (Racke)

unread,
Nov 25, 2020, 10:28:53 AM11/25/20
to ansible...@googlegroups.com
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


>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/dd7a6545-1c12-429a-9e8e-097e38ec7695n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/dd7a6545-1c12-429a-9e8e-097e38ec7695n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Vladimir Botka

unread,
Nov 25, 2020, 10:33:04 AM11/25/20
to Hiero-nymo, ansible...@googlegroups.com
On Wed, 25 Nov 2020 06:32:16 -0800 (PST)
Hiero-nymo <jer.m...@gmail.com> wrote:

> - 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 }}"
> ...
> Perhaps, there's another easiest solution??????

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"
]
}

--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages