How to bring up an interface using Ansible 'nmcli' module?

2.6k Views Asked by At

I can create an interface with something like:

- name: create dummy interface
  community.general.nmcli:
    type: dummy
    conn_name: '{{ item.conn_name }}'
    ifname: '{{ item.ifname }}'
    ip6: '{{ item.ip6 }}'
    state: present
  with_items:
    - '{{ nmcli_dummy }}'

But if in the server I put the interface down: ifconfig dummy0 down, what parameter or options could be used to manage the interface state, for example up or down?

1

There are 1 best solutions below

0
On

For a connection in example named eth1 the equivalents for show, up and down are

nmcli con show eth1
nmcli con up eth1
nmcli con down eth1

Whereby in nmcli module are certain Parameters for

Manage the network devices. Create, modify and manage various connection and device type e.g., ethernet, teams, bonds, vlans etc.

a parameter for bringing a network interface up or down is not explicit named.

This might leave one to think to workaround it with

- name: Brings the interface up or down
  command: 
    cmd: "nmcli con {{ CMD }} eth1"
  register: nmcli_con_cmd_result

as this is almost what the module code is doing under the hood.

However, according the NetworkManager / ansible-network-role it seems that the paramter state can have more values. In your case you could first check how it is implemented there in the project role and test after with state: up and state: down accordingly.

Regarding

I can create an interface with ...

it seems that if connection becomes created it is brought up, as well if connection becomes removed, it is brought down before.

Further Documentation