Automate Mikrotik RouterOS Config Backups with Ansible
By Luca Berton · Published 2024-01-01 · Category: installation
Learn how to automate configuration backups for Mikrotik RouterOS with Ansible. This guide includes a step-by-step Playbook example for seamless backups using.

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.See also: Automate Dell EMC DNOS 10 Backups with Ansible Playbook
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## 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[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
---
- 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
---
collections:
- name: community.routeros
requirements setup
$ 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.netcommon 3.0.0
ansible.utils 2.6.1
community.routeros 2.0.0
execution
$ ansible-playbook -i inventory backup_mikrotik.yml
PLAY [Backup Mikrotik] ****************************************************************************
TASK [Export] *************************************************************************************
changed: [192.168.88.2 -> localhost]
TASK [Backup] *************************************************************************************
changed: [192.168.88.2 -> localhost]
PLAY RECAP ****************************************************************************************
192.168.88.2 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
before execution
luca@luca01:~/network$ pwd
/home/luca/network
luca@luca01:~/network$ ls
backup_mikrotik.yml inventory requirements.yml
luca@luca01:~/network$ ansible --version
ansible [core 2.12.4]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/luca/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /home/luca/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0]
jinja version = 2.10.1
libyaml = True
after execution
$ ls -al backup
total 16
drwxrwxr-x 2 luca luca 4096 May 5 15:29 .
drwxr-xr-x 3 luca luca 4096 May 5 15:28 ..
-rw-rw-r-- 1 luca luca 2076 May 5 15:28 192.168.88.2_config.2022-05-05@15:28:55
-rw-rw-r-- 1 luca luca 2076 May 5 15:29 192.168.88.2_config.2022-05-05@15:29:19
luca@luca01:~/network$ file backup/*
backup/192.168.88.2_config.2022-05-05@15:28:55: ASCII text, with CRLF line terminators
backup/192.168.88.2_config.2022-05-05@15:29:19: ASCII text, with CRLF line terminators
See also: Backup Dell EMC DNOS 6 Configs with Ansible Playbook
Conclusion
Now you know how to Backup Config on Mikrotik RouterOS with Ansible.Related Articles
• Ansible Galaxy guide • Ansible conditional patterns • the Ansible inventory deep-diveCategory: installation
Watch the video: Automate Mikrotik RouterOS Config Backups with Ansible — Video Tutorial