Bootstrap FreeKB - Ansible - Update Network Manager IP Addresses using the nmcli module
Ansible - Update Network Manager IP Addresses using the nmcli module

Updated:   |  Ansible articles

nmcli can be used to add additional IP addresses to the systems network interface. Let's say nmcli connection show eth0 currently has the following IPv4 addresses.

]# nmcli connection show eth0 | grep -i ipv4
ipv4.addresses:                         10.11.12.15/24

 

And you run the following playbook.

---
- hosts: all
  tasks:
  - name: add IPv4 addresses to the eth0 interface
    community.general.nmcli:
      conn_name: eth0
      type: ethernet
      state: present
      ip4: 10.11.12.16/24

  - raw: nmcli device reapply eth0
...

 

nmcli connection show eth0 will the have only 10.11.12.16/22. In other words, the Ansible nmcli module will not append additional IP addresses. Instead, the Ansible nmcli module will update the nmcli ipv4.addresses to contain the IP addresses listed in the "ip4" key of the Ansible nmcli module.

]# nmcli connection show eth0 | grep -i ipv4
ipv4.addresses:                         10.11.12.16/24

 

You can use a string of IP addresses. Unfortunately the Ansible nmcli module does not have a way to update Network Manager to pick up the new IP address so I use the raw module to issue the nmcli device reapply command.

---
- hosts: all
  tasks:
  - name: add IPv4 addresses to the eth0 interface
    community.general.nmcli:
      conn_name: eth0
      type: ethernet
      state: present
      ip4: "10.11.12.16 10.11.12.17 10.11.12.18"

  - raw: nmcli device reapply eth0
...

 

You can use a string of IP addresses with prefix.

---
- hosts: all
  tasks:
  - name: add IPv4 addresses to the eth0 interface
    community.general.nmcli:
      conn_name: eth0
      type: ethernet
      state: present
      ip4: "10.11.12.16/24 10.11.12.17/24 10.11.12.18/24"

  - raw: nmcli device reapply eth0
...

 

You can use a list of IP addresses, with or without prefix.

---
- hosts: all
  vars:
    ip_addresses: ['10.11.12.16', '10.11.12.17/24', '10.11.12.18']
  tasks:
  - name: add IPv4 addresses to the eth0 interface
    community.general.nmcli:
      conn_name: eth0
      type: ethernet
      state: present
      ip4: "{{ ip_addresses }}"

  - raw: nmcli device reapply eth0
...

 

If you want to append additional IP addresses, you will want to first capture the list of IP addresses and prefix. Let's say you have the following playbook which outputs ansible_all_ipv4_addresses.

---
- hosts: all
  tasks:
  - debug:
      var: ansible_all_ipv4_addresses
...

 

Running this playbook should return something like this. Notice that ansible_all_ipv4_addresses does not contain prefix, such as 10.11.12.13/24. For this reason, I don't go with ansible_all_ipv4_addresses.

ok: [server1.example.com] => {
    "ansible_all_ipv4_addresses": [
        "10.11.12.13",
        "10.11.12.14",
        "10.11.12.15"
    ]
}

 

Instead, I use ansible_eth0 (assuming there is an eth0 interface on the target system) to get the list of IP addresses and the netmask bound to the eth0 interface.

---
- hosts: all
  tasks:
  - debug:
      var: ansible_eth0
...

 

Something like this should be returned.

ok: [server1.example.com] => {
    "ansible_eth0": {
        "active": true,
        "device": "eth0",
        "ipv4": {
            "address": "10.11.12.13",
            "broadcast": "10.0.127.255",
            "netmask": "255.255.255.0",
            "network": "10.0.0.0"
        },
        "ipv4_secondaries": [
            {
                "address": "10.11.12.14",
                "broadcast": "10.0.127.255",
                "netmask": "255.255.255.0",
                "network": "10.0.0.0"
            },
            {
                "address": "10.11.12.15",
                "broadcast": "10.0.127.255",
                "netmask": "255.255.255.0",
                "network": "10.0.0.0"
            }
        ]
    }
}

 

And then do the following to append the IP addresses with the prefix to a list.

- name: create the empty all_ipv4_addresses_with_prefix list
  set_fact:
    all_ipv4_addresses_with_prefix: []
  
- name: append default IPv4 address/prefix to the 'all_ipv4_addresses_with_prefix' list
  set_fact:
    all_ipv4_addresses_with_prefix: "{{ all_ipv4_addresses_with_prefix + [(ansible_eth0.ipv4.address + '/' + ansible_eth0.ipv4.netmask) | ansible.utils.ipaddr('network/prefix') ] }}"

- name: append secondary IPv4 addresses/prefix to the 'all_ipv4_addresses_with_prefix' list
  set_fact:
    all_ipv4_addresses_with_prefix: "{{ all_ipv4_addresses_with_prefix + [(item.address + '/' + item.netmask) | ansible.utils.ipaddr('network/prefix') ] }}"
    with_items: "{{ ansible_eth0.ipv4_secondaries }}"
  when: ansible_eth0.ipv4_secondaries is defined

 

The all_ipv4_addresses_with_prefix list should contain something like this.

ok: [server1.example.com] => {
    "all_ipv4_addresses_with_prefix": [
        "10.11.12.13/24",
        "10.11.12.14/24",
        "10.11.12.15/24"
    ]
}

 

And then you could append one or more IP addresses with prefix to the all_ipv4_addresses_with_prefix list.

- name: append ip address/prefix to the all_ipv4_addresses_with_prefix list
  set_fact:
    all_ipv4_addresses_with_prefix : "{{ all_ipv4_addresses_with_prefix + ['10.11.12.16/24'] }}"

 

Or to remove an IPv4 address.

- name: Remove IPv4 address from the all_ipv4_addresses_with_prefix list
  set_fact:
    all_ipv4_addresses_with_prefix : "{{ all_ipv4_addresses_with_prefix  | reject('search', '10.11.12.14/24') | list }}"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a82db3 in the box below so that we can be sure you are a human.