-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_configs_local_groups.yaml
95 lines (82 loc) · 3.16 KB
/
backup_configs_local_groups.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
# 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
# - device target_group from ansible inventory
---
- name: Backup configurations from network devices
hosts: "{{ target_group | default('all') }}"
gather_facts: no
strategy: ansible.builtin.linear #change default from free to 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 ~/.ssh/id_rsa"
repo: "{{ git_repo_url }}"
dest: "{{ tempdir.path }}"
run_once: true
- name: ensure configuration directory exists
ansible.builtin.file:
path: "{{ tempdir.path }}/{{ group_names[0] }}"
state: directory
run_once: true
- name: Gather configuration from Cisco IOS devices
when: ansible_network_os == 'ios'
cisco.ios.ios_command:
commands:
- show running-config
register: ios_config
- name: Gather configuration from Arista EOS devices
when: ansible_network_os == 'eos'
arista.eos.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: "missing required variable: device_config"
when: device_config is undefined or device_config == ""
- name: Save configuration to file
ansible.builtin.copy:
content: "{{ device_config.stdout[0] }}"
dest: "{{ tempdir.path }}/{{ group_names[0] }}/{{ inventory_hostname_short }}.txt"
#dest: "{{ tempdir }}/{{ 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 ~/.ssh/id_rsa"
run_once: true
always:
- name: Delete temp directory
ansible.builtin.file:
path: "{{ tempdir.path }}"
state: absent
run_once: true