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