-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.yml
57 lines (48 loc) · 2.08 KB
/
main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
---
- hosts: mikrotik
gather_facts: no
tasks:
- name: Set ssh port (set defualt 22 port when port is not defined)
set_fact:
ansible_port: "{{ ansible_port | default(22) }}"
- name: Run backup with password (when password is defined)
when: ansible_ssh_pass is defined
block:
- name: Ensure that sshpass is installed
ansible.builtin.shell: whereis sshpass | awk '{print $2}'
run_once: yes
register: sshpass
delegate_to: localhost
- ansible.builtin.fail:
msg: >-
Playbook requrie sshpass to use password authenication, please install
sshpass and re-run same playbook again.
You can found details in https://www.redhat.com/sysadmin/ssh-automation-sshpass
when: sshpass.stdout|length == 0
- name: gather export (with password authentication)
# shell with delegate_to: localhost works better that ansible.builtin.raw
ansible.builtin.shell: >-
sshpass -p '{{ ansible_ssh_pass }}' ssh -o StrictHostKeyChecking=no {{ ansible_user }}@{{ inventory_hostname }} -p {{ ansible_port }} /export
register: export
delegate_to: localhost
- name: store export to local file
ansible.builtin.copy:
content: "{{ export.stdout }}"
dest: "output/{{ inventory_hostname }}.config"
when: export is defined
delegate_to: localhost
- name: Run backup withot password (when password is not defined)
when: ansible_ssh_pass is not defined
block:
- name: gather export (with passwordless authentication)
# shell with delegate_to: localhost works better that ansible.builtin.raw
ansible.builtin.shell: >-
ssh {{ ansible_user }}@{{ inventory_hostname }} -p {{ ansible_port }} /export
register: export
delegate_to: localhost
- name: store export to local file
ansible.builtin.copy:
content: "{{ export.stdout }}"
dest: "output/{{ inventory_hostname }}.config"
when: export is defined
delegate_to: localhost