How to set an ipv4 default gateway with ansible nmcli in RHEL9?

90 Views Asked by At

I'm writing a playbook to set the default gateway on a server with RHEL9 and multiple network interfaces.

This is part of a repeated process, we're cloning a VM from a template with default gateway on ens192 (the management interface) and during the customization we set up routing and change the default gateway interface to another, typically ens224.

However, after running the nmcli module with the gw4 setting, the routing table is not updated.

This should theoretically work:

    - name: "unset default gw"
      community.general.nmcli:
        conn_name: "ens192"
        state: present
        never_default4: true
        dns4_ignore_auto: true

    - name: "set default gw"
      community.general.nmcli:
        conn_name: "ens224"
        state: present
        gw4: '10.59.41.1'
        dns4_ignore_auto: true

    - name: "reload changed NICs"
      shell: "/usr/bin/nmcli connection up {{ item }}"
      with_items:
        - ens192
        - ens224

The nmcli connection up trick is the same we use to apply other routing changes.

After running these tasks (ansible with --diff shows the proper changes) the routing table is not reloaded, we're just left with no default routes.

nmcli shows that both NICs have lost the gateway setting. Some times, restarting NetworkManager will reload everything properly, but it's not always consistent.

# nmcli con show ens192 | grep gateway ; nmcli con show ens224 | grep gateway
connection.gateway-ping-timeout:        0
ipv4.gateway:                           --
ipv6.gateway:                           --
connection.gateway-ping-timeout:        0
ipv4.gateway:                           --
ipv6.gateway:                           --

We're running ansible [core 2.15.9] with these collections:

Collection            Version
--------------------- -------
ansible.netcommon     6.0.0
ansible.posix         1.5.4
ansible.utils         3.1.0
community.crypto      2.17.1
community.general     8.3.0
community.hashi_vault 6.1.0
community.vmware      4.1.0

What is wrong here? Does the nmcli module not do what we are expecting from it?

0

There are 0 best solutions below