How to Backup Config on Mikrotik RouterOS with Ansible?
Maintaining a backup copy of your network appliance configuration is a good practice for all IT Professionals. You could automate this process for Mikrotik RouterOS appliances using Ansible.
I'm going to show you a live Playbook with some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible Backup Config on Mikrotik RouterOS
> community.routeros: Modules for MikroTik RouterOS
Let's talk about the Ansible collection community.routeros.
The full name is community.routeros, which means that is an Ansible Network Collection designed to interact with Mikrotik RouterOS devices.
It contains modules for MikroTik RouterOS.
Links
- [Community.Routeros](https://docs.ansible.com/ansible/latest/collections/community/routeros/index.html)
## Playbook
How to Backup Config on Mikrotik RouterOS with Ansible Playbook.
I'm going to show how to back up the current configuration of a Mikrotik RouterOS connecting via SSH protocol using the username and password credentials and save it to a file with the device name and timestamp.
code
- inventory
``ini
[mikrotik]
192.168.88.2
[mikrotik:vars]
ansible_user=luca
ansible_password=mysecretpassword123
ansible_connection=ansible.netcommon.network_cli
ansible_network_os=community.routeros.routeros
ansible_python_interpreter=/usr/bin/python3
ansible_command_timeout=120
`
- backup_mikrotik.yml
`yaml
---
- name: Backup Mikrotik
hosts: mikrotik
gather_facts: false
vars:
backup_dir: "/home/luca/network/backup"
tasks:
- name: Export
ansible.builtin.shell: >-
sshpass -p '{{ ansible_password }}' ssh -o StrictHostKeyChecking=no {{ ansible_user }}@{{ inventory_hostname }} /export
register: export
delegate_to: localhost
- name: Backup
ansible.builtin.copy:
content: "{{ export.stdout }}"
dest: "{{ backup_dir }}/{{ inventory_hostname }}_config.{{ lookup('pipe', 'date +%Y-%m-%d@%H:%M:%S') }}"
when: export is defined
delegate_to: localhost
`
- requirements.yml
`yaml
---
collections:
- name: community.routeros
`
requirements setup
``bash
$ ansible-galaxy install -r requirements.yml
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/download/community-routeros-2.0.0.tar.gz to /home/luca/.ansible/tmp/ansible-local-47086a9aez7_s/tmpqz_6puy_/community-routeros-2.0.0-hlve2za9
Installing 'community.routeros:2.0.0' to '/home/luca/.ansible/collections/ansible_collections/community/routeros'
community.routeros:2.0.0 was installed successfully
'ansible.netcommon:3.0.0' is already installed, skipping.
'ansible.utils:2.6.1' is already installed, skipping.
luca@luca01:~/network$ ansible-galaxy collection list
/home/luca/.ansible/collections/ansible_collections
Collection Version
------------------ -------
ansible.netc