-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_configs_array_group.yaml
102 lines (87 loc) · 3.5 KB
/
backup_configs_array_group.yaml
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# This playbook will back up the running configuration from Cisco IOS or Arista
# EOS devices. The following arguments must be set at runtime in order for
# this playbook to run successfully
#
# - git_repo_url: The URL of the Git repository to commit the configurations to
# - git_repo_branch: The Git branch to use
---
- name: Backup configurations from network devices
hosts: "{{ target_group | default('all') }}"
gather_facts: no
strategy: ansible.builtin.linear
pre_tasks:
- name: fail if git_repo_url is not defined
ansible.builtin.fail:
msg: "missing required variable: git_repo_url"
when: git_repo_url is undefined or git_repo_url == ""
run_once: true
- name: fail if git_repo_branch is not defined
ansible.builtin.fail:
msg: "missing required variable: git_repo_branch"
when: git_repo_branch is undefined or git_repo_url == ""
run_once: true
tasks:
- name: Backup device configurations and commit to git
block:
- name: create temp directory
ansible.builtin.tempfile:
state: directory
register: tempdir
run_once: true
- name: clone existing repository
ansible.builtin.git:
key_file: "ssh -i /opt/custom/ssh/id_rsa"
repo: "{{ git_repo_url }}"
dest: "{{ tempdir.path }}"
run_once: true
- name: ensure configuration directory exists
ansible.builtin.file:
path: "{{ tempdir.path }}/configurations/{{ group_names[0] }}"
state: directory
run_once: true
- name: Check if target_devices is defined
assert:
that: target_devices is not defined or target_devices | length == 0 or inventory_hostname in target_devices
fail_msg: "This host is not in the target_devices list"
- name: Gather configuration from Cisco IOS devices
when:
- ansible_network_os == 'ios'
- target_devices is not defined or target_devices | length == 0 or inventory_hostname in target_devices
ios_command:
commands:
- show running-config
register: ios_config
- name: Gather configuration from Arista EOS devices
when:
- ansible_network_os == 'eos'
- target_devices is not defined or target_devices | length == 0 or inventory_hostname in target_devices
eos_command:
commands:
- show running-config
register: eos_config
- name: Set device_config
set_fact:
device_config: "{{ ios_config | combine(eos_config) }}"
- name: fail if device_config is not defined
ansible.builtin.fail:
msg: "Something went wrong gathering: device_config"
when: device_config is undefined or device_config == ""
- name: Save device_config to a file
ansible.builtin.copy:
content: "{{ device_config.stdout[0] }}"
dest: "{{ tempdir.path }}/{{ ansible_network_os }}/{{ inventory_hostname_short }}.txt"
- name: Add updated files to git
ansible.builtin.shell: |
git add .
git commit -m 'Backup configurations'
git push -u origin {{ git_repo_branch }}
args:
chdir: "{{ tempdir.path }}"
environment:
GIT_SSH_COMMAND: "ssh -i /opt/custom/ssh/id_rsa"
always:
- name: Delete temp directory
ansible.builtin.file:
path: "{{ tempdir.path }}"
state: absent
run_once: true