-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.yml
101 lines (87 loc) · 2.79 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
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
---
- name: 从DNAC获取设备配置、以及设备的uuid,demo playbook 1
hosts: dnac_servers
vars_files:
- credentials.yml
gather_facts: no
tasks:
- name: Get all network device info for this family - switches
cisco.dnac.network_device_info:
dnac_host: "{{dnac_host}}"
dnac_username: "{{dnac_username}}"
dnac_password: "{{dnac_password}}"
dnac_verify: "{{dnac_verify}}"
dnac_port: "{{dnac_port}}"
dnac_version: "{{dnac_version}}"
family: "Switches and Hubs"
register: result
- name: Json Query
set_fact:
hostname_id:
"{{ result.dnac_response | json_query('response[*].{hostname: hostname, id: id}') }}"
# - name: Show Json Query
# debug:
# var: hostname_id
- name: Get config file for device with ID
cisco.dnac.network_device_config_info:
dnac_host: "{{dnac_host}}"
dnac_username: "{{dnac_username}}"
dnac_password: "{{dnac_password}}"
dnac_verify: "{{dnac_verify}}"
dnac_port: "{{dnac_port}}"
dnac_version: "{{dnac_version}}"
networkDeviceId: "{{ item }}"
register: config_result
loop: "{{ device_uuid }}"
- name: set config var.
set_fact:
config_list:
"{{ config_result.results }}"
# - name: config list 原始信息
# debug:
# msg: "{{ config_list }}"
# - name: Show result
# debug:
# msg: "{{ item.dnac_response.response }}"
# with_items: "{{ config_list }}"
- name: SAVE config to file
local_action: copy content="{{ item.dnac_response.response }}" dest="backup/show_run_{{ item.item }}.txt"
with_items: "{{ config_list }}"
loop_control:
label: "For device id: {{ item.item }}"
- name: REMOVE CONFIG LINES WITH LIST
lineinfile:
path: "backup/show_run_{{ item.1 }}.txt"
state: absent
regexp: '^{{ item.0 }}'
delegate_to: localhost
with_nested:
- "{{ lines_removed }}"
- "{{ device_uuid }}"
- name: 配置备份文件同步到远端服务器 - Github,demo playbook 2
hosts: localhost
connection: local
tags: git
ignore_errors: true
tasks:
- name: 文件更新至local git repo,检查是否有变化
shell: |
git add --all
git diff-index --quiet HEAD --;
register: git_result
run_once: true
# - name: debug output
# debug:
# msg: "{{ git_result }}"
- name: 如果文件没变化,则仅仅显示信息“文件没有变化”
debug:
msg: "文件没有变化,不需要 git commit"
when: not git_result.failed
run_once: true
- name: 如果文件有变化,则同步到 Github
shell: |
git commit -m "Updates config from ansible auto at {{ ansible_date_time.date }}"
git push origin main -f
register: gitcommit_result
run_once: true
when: git_result.failed