What's the "best" way to add an ip route using ansible?
05:15 04 Jan 2023

I am unable to find an Ansible module that adds IP routes.

Basically the command I'm looking for should allow something like:

sudo ip route add 12.3.4.0/24 via 123.456.78.90

I found net_static_route module which seems related, but is deprecated since 2022-06-01 and removed in Ansible v2.13.

A simple solution would be:

- name: Add a route
  ansible.builtin.shell:
    cmd: sudo ip route add 12.3.4.0/24 via 123.456.78.90

of course, but dedicated modules are usually better.

Additional Information

This task will be executed on a subset of all nodes (Ubuntu 22.04 machines that are all part of a cloud cluster using OpenStack) (specified by groups). Concrete subnets and IPs will be defined using variables.

An example hostfile could look like this:

master:
  hosts:
    localhost:
      ansible_connection: local
      ansible_python_interpreter: /usr/bin/python3
      ansible_user: ubuntu
      ip: localhost
workers:
  children:
    ephemeral:
      hosts: {}
  hosts:
    someworker:
      ansible_connection: ssh
      ansible_python_interpreter: /usr/bin/python3
      ansible_user: ubuntu

This playbook will be used to setup WireGuard. Every VPN node (those nodes will be connected with each other via WireGuard) is connected in its own subnet with multiple worker nodes. Those workers need to add the IP route to get back to the master node which is in a different subnet, but in the same virtual subnet created by WireGuard and the VPN nodes). I do not think that this is related to my question, but I might overlook how this can affect the right answer.

ansible