Files
ansible/mikrotikbackup.yml
2025-07-19 16:37:35 +00:00

71 lines
2.3 KiB
YAML

- hosts: mikrotiks
gather_facts: no
tasks:
- name: Set SSH port (default to 22)
set_fact:
ansible_port: "{{ ansible_port | default(22) }}"
- name: Ensure output directory exists
ansible.builtin.file:
path: output
state: directory
mode: '0755'
delegate_to: localhost
- name: Get router identity
ansible.builtin.shell: >
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no
{{ ansible_user }}@{{ ansible_host }} -p {{ ansible_port }}
"/system identity print"
register: system_identity
delegate_to: localhost
- name: Set router name
set_fact:
router_name: "{{ system_identity.stdout.split(': ')[1] | trim }}"
- name: Generate current date
ansible.builtin.shell: date +%Y-%m-%d
register: date_output
delegate_to: localhost
- name: Set current date
set_fact:
current_date: "{{ date_output.stdout }}"
- name: Export router config
ansible.builtin.shell: >
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no
{{ ansible_user }}@{{ ansible_host }} -p {{ ansible_port }}
"/export"
register: export_output
delegate_to: localhost
- name: Save export to local file
ansible.builtin.copy:
content: "{{ export_output.stdout }}"
dest: "output/{{ router_name }}-{{ current_date }}.config"
delegate_to: localhost
- name: Create binary backup on router
ansible.builtin.shell: >
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no
{{ ansible_user }}@{{ ansible_host }} -p {{ ansible_port }}
"/system backup save name={{ router_name }}-{{ current_date }}-backup"
delegate_to: localhost
- name: Download binary backup
ansible.builtin.shell: >
scp -o ConnectTimeout=10 -o StrictHostKeyChecking=no
-P {{ ansible_port }}
{{ ansible_user }}@{{ ansible_host }}:{{ router_name }}-{{ current_date }}-backup.backup
output/
delegate_to: localhost
- name: Remove backup file from router
ansible.builtin.shell: >
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no
{{ ansible_user }}@{{ ansible_host }} -p {{ ansible_port }}
"/file remove {{ router_name }}-{{ current_date }}-backup.backup"
delegate_to: localhost