Ansible Mikrotik Backup Configuration

$ vi ansible.cfg

[defaults]
inventory = $HOME/ansible/hosts
remote_tmp = $HOME/ansible/tmp/ansible-${USER}/tmp
local_tmp = $HOME/ansible/tmp/ansible-${USER}/tmp
host_key_checking = False
timeout = 15
log_path = $HOME/ansible/ansible.log

$ vi hosts

[mikrotik]
192.168.21.21 ansible_host=192.168.21.21
192.168.116.116 ansible_host=192.168.116.116

[mikrotik:vars]
ansible_user = agink
ansible_password = xxxPas5worDxxx
# ansible_private_key_file = ~/.ssh/id_macos
ansible_port = 2121
ansible_connection = network_cli
ansible_network_os = routeros
gather_facts = false

$ vi mikrotik_backup.yml

---
- name: AGINK - MikroTik Backup Automation
  hosts: mikrotik
  connection: local
  gather_facts: no
  ignore_errors: yes

  vars:
     date: "{{ lookup('pipe', 'date +%Y%m%d_%H%M') }}"
     backup_path: "~/ansible/backup"

  tasks:
     - name: Backup filename.backup
       routeros_command:
         commands:
             - /system backup save name={{ ansible_host }}

     - name: Backup filename.rsc
       routeros_command:
         commands:
             - /export file={{ ansible_host }}

     - name: Download Backup files to localhost
       with_items:
             - "{{ ansible_host }}.rsc"
             - "{{ ansible_host }}.backup"
       command: sshpass -p {{ ansible_password }} scp -o StrictHostKeyChecking=no -P {{ ansible_port }} {{ ansible_user }}@{{ ansible_host }}:{{ item }} {{ backup_path }}/{{ date }}_{{ item }}
       delegate_to: localhost

$ ansible-playbook mikrotik_backup.yml

PLAY [AGINK - MikroTik Backup Automation] ***********************************************************************************************************

TASK [Backup filename.backup] ***********************************************************************************************************************
ok: [192.168.116.116]
ok: [192.168.21.21]

TASK [Backup filename.rsc] **************************************************************************************************************************
ok: [192.168.21.21]
ok: [192.168.116.116]

TASK [Download Backup filename to localhost] ********************************************************************************************************
changed: [192.168.116.116 -> localhost] => (item=192.168.116.116.rsc)
changed: [192.168.21.21 -> localhost] => (item=192.168.21.21.rsc)
changed: [192.168.116.116 -> localhost] => (item=192.168.116.116.backup)
changed: [192.168.21.21 -> localhost] => (item=192.168.21.21.backup)

PLAY RECAP ******************************************************************************************************************************************
192.168.116.116 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 
192.168.21.21 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0