Back up RouterOS and Cisco configurations with Ansible

Collect configurations automatically and keep a Git history of changes.

A versioned backup lets you answer what changed before an outage. Store exports as text so you can compare each device with its previous revision.

Inventory

# inventory.yml
all:
  children:
    routeros:
      hosts: { r-office-1: { ansible_host: 10.10.10.2 } }
      vars: { ansible_network_os: community.routeros.routeros }
    ios:
      hosts: { sw-core-1: { ansible_host: 10.10.10.5 } }
      vars: { ansible_network_os: cisco.ios.ios }
  vars:
    ansible_connection: ansible.netcommon.network_cli
    ansible_user: backup

Playbook

# backup.yml
- hosts: routeros
  gather_facts: false
  tasks:
    - community.routeros.command: { commands: ["/export terse show-sensitive"] }
      register: out
    - ansible.builtin.copy:
        content: "{{ out.stdout[0] | regex_replace('^# .* by RouterOS.*\\n', '') }}"
        dest: "configs/{{ inventory_hostname }}.rsc"
      delegate_to: localhost

- hosts: ios
  gather_facts: false
  tasks:
    - cisco.ios.ios_command: { commands: ["show running-config"] }
      register: out
    - ansible.builtin.copy:
        content: "{{ out.stdout[0] | regex_replace('(?m)^(! Last configuration change|ntp clock-period).*\\n', '') }}"
        dest: "configs/{{ inventory_hostname }}.cfg"
      delegate_to: localhost
Removing export noise

An export date changes even when the configuration does not. Filter those lines so the Git history tracks meaningful changes.

Commit the changes

#!/bin/sh
cd /opt/netbackup
ansible-playbook -i inventory.yml backup.yml -f 10 || exit 1
git add -A configs
git diff --cached --quiet || git commit -qm "config backup $(date +%F)"

Schedule the backup with a systemd timer and use a dedicated read-only account on the devices. Keep the resulting repository private because exports can contain sensitive configuration.