How to Add/Delete Static Routes Using Ansible Playbooks: Complete Guide with Code Examples


4 views

While Ansible doesn't have dedicated modules for route management, we can leverage existing system modules to achieve this. The most common approaches involve:


- Using the command or shell module for direct route commands
- Utilizing OS-specific modules like win_route for Windows
- Creating persistent routes through configuration files

Here's how to add a temporary static route on Linux systems:


- name: Add temporary static route
  become: yes
  ansible.builtin.command: |
    ip route add 192.168.1.0/24 via 10.0.0.1 dev eth0

For permanent routes on Debian/Ubuntu systems:


- name: Configure persistent static route
  become: yes
  blockinfile:
    path: /etc/network/interfaces.d/route-eth0
    block: |
      up ip route add 192.168.1.0/24 via 10.0.0.1 dev eth0
      down ip route del 192.168.1.0/24 via 10.0.0.1 dev eth0

For Windows systems using the win_route module:


- name: Add route on Windows
  ansible.windows.win_route:
    destination: 192.168.1.0
    netmask: 255.255.255.0
    gateway: 10.0.0.1
    metric: 1
    state: present

To remove a route across different platforms:


# Linux temporary route removal
- name: Remove temporary route
  become: yes
  command: ip route del 192.168.1.0/24 via 10.0.0.1

# Windows route removal
- name: Remove Windows route
  ansible.windows.win_route:
    destination: 192.168.1.0
    netmask: 255.255.255.0
    state: absent

When managing routes with Ansible:


1. Always use handler for network service restarts
2. Implement idempotency checks with 'changed_when'
3. Consider using templates for complex routing setups
4. Document all route changes in playbook comments

While Ansible doesn't have dedicated modules for static route operations, we can leverage existing system modules combined with shell commands to achieve this. The key is understanding whether you're working with persistent routes (that survive reboots) or temporary runtime routes.

For temporary route additions that won't persist after reboot:


- name: Add temporary static route
  ansible.builtin.command: ip route add 192.168.1.0/24 via 10.0.0.1 dev eth0

To remove the route:


- name: Remove temporary static route
  ansible.builtin.command: ip route del 192.168.1.0/24

For Linux systems, we typically modify network configuration files. Here's how to do it with Ansible:


- name: Configure persistent static route
  ansible.builtin.lineinfile:
    path: /etc/sysconfig/network-scripts/route-eth0
    line: "192.168.2.0/24 via 10.0.0.2"
    create: yes
  notify: restart network

With the corresponding handler:


- name: restart network
  ansible.builtin.service:
    name: network
    state: restarted

For Windows targets, use the win_command module:


- name: Add Windows static route
  ansible.windows.win_command: route add 192.168.3.0 mask 255.255.255.0 10.0.0.3
  • Always document your route changes in playbook comments
  • Use variables for gateway addresses to make playbooks reusable
  • Consider idempotency - check if route exists before adding
  • For complex networks, create separate route management playbooks

- hosts: network_devices
  vars:
    new_route: 192.168.4.0/24
    gateway: 10.0.0.4
    interface: eth0

  tasks:
    - name: Check if route exists
      ansible.builtin.command: ip route show {{ new_route }}
      register: route_check
      ignore_errors: yes
      changed_when: false

    - name: Add route if not present
      ansible.builtin.command: ip route add {{ new_route }} via {{ gateway }} dev {{ interface }}
      when: route_check.rc != 0