--- - hosts: mikrotik_routers gather_facts: no tasks: - name: Set SSH port (set default 22 port when port is not defined) 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: 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 %} 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: 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 %} register: export_output delegate_to: localhost - name: Store export to local file with date 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 %} 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 %} 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