Update mikrotikbackup.yml

This commit is contained in:
2025-07-19 16:28:24 +00:00
parent b60d3a5720
commit fefb4badee

View File

@@ -1,8 +1,7 @@
--- - hosts: mikrotik_routers
- hosts: mikrotiks
gather_facts: no gather_facts: no
tasks: tasks:
- name: Set SSH port (set default 22 port when port is not defined) - name: Set SSH port (default to 22)
set_fact: set_fact:
ansible_port: "{{ ansible_port | default(22) }}" ansible_port: "{{ ansible_port | default(22) }}"
@@ -13,13 +12,12 @@
mode: '0755' mode: '0755'
delegate_to: localhost delegate_to: localhost
- name: Gather system identity (router name) using SSH key or password - name: Get router identity
ansible.builtin.shell: | ansible.builtin.shell: >
{% if ansible_ssh_private_key_file is defined %} ssh -o StrictHostKeyChecking=no
ssh -o StrictHostKeyChecking=no -i {{ ansible_ssh_private_key_file }} {{ ansible_user }}@{{ hostvars[inventory_hostname]['ansible_host'] }} -p {{ ansible_port }} "/system identity print" {{ ansible_user }}@{{ ansible_host }}
{% else %} -p {{ ansible_port }}
sshpass -p '{{ ansible_ssh_pass }}' ssh -o StrictHostKeyChecking=no {{ ansible_user }}@{{ hostvars[inventory_hostname]['ansible_host'] }} -p {{ ansible_port }} "/system identity print" "/system identity print"
{% endif %}
register: system_identity register: system_identity
delegate_to: localhost delegate_to: localhost
@@ -28,7 +26,7 @@
router_name: "{{ system_identity.stdout.split(': ')[1] | trim }}" router_name: "{{ system_identity.stdout.split(': ')[1] | trim }}"
- name: Generate current date - name: Generate current date
ansible.builtin.shell: "date +%Y-%m-%d" ansible.builtin.shell: date +%Y-%m-%d
register: date_output register: date_output
delegate_to: localhost delegate_to: localhost
@@ -36,46 +34,41 @@
set_fact: set_fact:
current_date: "{{ date_output.stdout }}" current_date: "{{ date_output.stdout }}"
- name: Run export command to gather configuration using SSH key or password - name: Export router config
ansible.builtin.shell: | ansible.builtin.shell: >
{% if ansible_ssh_private_key_file is defined %} ssh -o StrictHostKeyChecking=no
ssh -o StrictHostKeyChecking=no -i {{ ansible_ssh_private_key_file }} {{ ansible_user }}@{{ hostvars[inventory_hostname]['ansible_host'] }} -p {{ ansible_port }} "/export" {{ ansible_user }}@{{ ansible_host }}
{% else %} -p {{ ansible_port }}
sshpass -p '{{ ansible_ssh_pass }}' ssh -o StrictHostKeyChecking=no {{ ansible_user }}@{{ hostvars[inventory_hostname]['ansible_host'] }} -p {{ ansible_port }} "/export" "/export"
{% endif %}
register: export_output register: export_output
delegate_to: localhost delegate_to: localhost
- name: Store export to local file with date - name: Save export to local file
ansible.builtin.copy: ansible.builtin.copy:
content: "{{ export_output.stdout }}" content: "{{ export_output.stdout }}"
dest: "output/{{ router_name }}-{{ current_date }}.config" dest: "output/{{ router_name }}-{{ current_date }}.config"
when: export_output is defined
delegate_to: localhost delegate_to: localhost
- name: Create binary backup on the router using SSH key or password - name: Create binary backup on router
ansible.builtin.shell: | ansible.builtin.shell: >
{% if ansible_ssh_private_key_file is defined %} ssh -o StrictHostKeyChecking=no
ssh -o StrictHostKeyChecking=no -i {{ ansible_ssh_private_key_file }} {{ ansible_user }}@{{ hostvars[inventory_hostname]['ansible_host'] }} -p {{ ansible_port }} "/system backup save name={{ router_name }}-{{ current_date }}-backup" {{ ansible_user }}@{{ ansible_host }}
{% else %} -p {{ ansible_port }}
sshpass -p '{{ ansible_ssh_pass }}' ssh -o StrictHostKeyChecking=no {{ ansible_user }}@{{ hostvars[inventory_hostname]['ansible_host'] }} -p {{ ansible_port }} "/system backup save name={{ router_name }}-{{ current_date }}-backup" "/system backup save name={{ router_name }}-{{ current_date }}-backup"
{% endif %}
delegate_to: localhost delegate_to: localhost
- name: Download binary backup file from the router using SSH key or password - name: Download binary backup
ansible.builtin.shell: | ansible.builtin.shell: >
{% if ansible_ssh_private_key_file is defined %} scp -o StrictHostKeyChecking=no
scp -o StrictHostKeyChecking=no -i {{ ansible_ssh_private_key_file }} -P {{ ansible_port }} {{ ansible_user }}@{{ hostvars[inventory_hostname]['ansible_host'] }}:{{ router_name }}-{{ current_date }}-backup.backup output/ -P {{ ansible_port }}
{% else %} {{ ansible_user }}@{{ ansible_host }}:{{ router_name }}-{{ current_date }}-backup.backup
sshpass -p '{{ ansible_ssh_pass }}' scp -o StrictHostKeyChecking=no -P {{ ansible_port }} {{ ansible_user }}@{{ hostvars[inventory_hostname]['ansible_host'] }}:{{ router_name }}-{{ current_date }}-backup.backup output/ output/
{% endif %}
delegate_to: localhost delegate_to: localhost
- name: Remove binary backup file from the router using SSH key or password - name: Remove backup file from router
ansible.builtin.shell: | ansible.builtin.shell: >
{% if ansible_ssh_private_key_file is defined %} ssh -o StrictHostKeyChecking=no
ssh -o StrictHostKeyChecking=no -i {{ ansible_ssh_private_key_file }} {{ ansible_user }}@{{ hostvars[inventory_hostname]['ansible_host'] }} -p {{ ansible_port }} "/file remove {{ router_name }}-{{ current_date }}-backup.backup" {{ ansible_user }}@{{ ansible_host }}
{% else %} -p {{ ansible_port }}
sshpass -p '{{ ansible_ssh_pass }}' ssh -o StrictHostKeyChecking=no {{ ansible_user }}@{{ hostvars[inventory_hostname]['ansible_host'] }} -p {{ ansible_port }} "/file remove {{ router_name }}-{{ current_date }}-backup.backup" "/file remove {{ router_name }}-{{ current_date }}-backup.backup"
{% endif %} delegate_to: localhost
delegate_to: localhost