forked from AutomateIP/ansible_ios_eos_device_backups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_configs_local_groups.yaml
82 lines (69 loc) · 2.69 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
# 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
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 == ""
- 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 == ""
tasks:
- name: Backup device configurations and commit to git
block:
- name: create temp directory
ansible.buildin.tempfile:
state: directory
register: tempdir
- name: clone existing repository
ansible.builtin.git:
repo: "{{ git_repo_url }}"
dest: "{{ tempdir.path }}"
- name: ensure configuration directory exists
ansible.builtin.file:
path: "{{ tempdir.path }}/configurations/{{ group_names[0] }}"
state: directory
- name: Gather configuration from Cisco IOS devices
when: ansible_network_os == 'ios'
cisco.ios.ios_command:
commands:
- show running-config
register: device_config
- name: Gather configuration from Arista EOS devices
when: ansible_network_os == 'eos'
arista.eos.eos_command:
commands:
- show running-config
register: device_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.buildin.copy:
content: "{{ ios_config.stdout[0] }}"
dest: "{{ tempdir.path }}/configurations/{{ group_names[0] }}/{{ inventory_hostname_short }}.txt"
- name: Add updated files to git
ansible.builtin.command:
command: "{{ item }}"
chdir: "{{ tempdir.path }}"
environment:
GIT_SSH_COMMAND: "ssh -i /opt/custom/ssh/id_rsa"
with_items:
- "git add ."
- "git commit -m 'Backup configurations'"
- "git push -u origin {{ git_repo_branch }}"
always:
- name: Delete temp directory
ansible.builtin.file:
path: "{{ tempdir.path }}"
state: absent