-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology.yml
116 lines (112 loc) · 3.9 KB
/
topology.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Ansible playbook to create a topology diagram from CDP information
# Copyright (C) 2018 Erik Auerswald <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
---
- name: 'Retrieve CDP neighbor information'
hosts: OOB
connection: local
vars:
cdp_info_dir: 'cdp_data'
connectivity_prefix: 'connectivity'
out_dir: 'graph'
tasks:
- name: 'Query devices via OOB interface'
tags: [ 'get' ]
ios_command:
commands: 'show cdp neighbors detail'
register: cdp
- name: '+++ DEBUG +++ Print CDP neighbor information'
tags: [ 'debug_get' ]
debug: var=cdp
- name: 'Delete directory for CDP neighbor information'
tags: [ 'get' ]
run_once: true
file:
path: '{{cdp_info_dir}}'
state: absent
- name: 'Create directory for CDP neighbor information'
tags: [ 'get' ]
run_once: true
file:
path: '{{cdp_info_dir}}'
state: directory
- name: 'Write CDP neighbor information to file'
tags: [ 'get' ]
copy:
# the empty line at the end ensures a newline at the end of the last
# output line (required for POSIX text files)
content: |
{{cdp.stdout[0]}}
{{''}}
dest: '{{cdp_info_dir}}/{{inventory_hostname}}'
- name: 'Read CDP neighbor information from file'
tags: [ 'parse' ]
set_fact:
cdp_data: '{{lookup("file", cdp_info_dir + "/" + inventory_hostname)}}'
- name: '+++ DEBUG +++ Print CDP data read from file'
tags: [ 'debug_parse' ]
debug: var=cdp_data
- name: 'Parse CDP data using TextFSM'
tags: [ 'parse' ]
set_fact:
cdp_neighbors: >
{{cdp_data |
parse_cli_textfsm("playbooks/cdp_neighbors_detail.textfsm")}}
- name: '+++ DEBUG +++ Parse CDP data with TextFSM and print result'
tags: [ 'debug_parse' ]
debug: var=cdp_neighbors
- name: '+++ DEBUG +++ Write Ansible variables to a file'
tags: [ 'debug_ansible' ]
run_once: true
copy:
content: |
Module Variables (vars):
------------------------
{{vars | to_nice_yaml}}
Environment Variables (environment):
------------------------------------
{{environment | to_nice_yaml}}
Group Names (group_names):
--------------------------
{{group_names | to_nice_yaml}}
Host Variables (hostvars):
--------------------------
{{hostvars | to_nice_yaml}}
dest: './debug-ansible_variables.yml'
- name: 'Delete directory for graph files'
tags: [ 'graph' ]
run_once: true
file:
path: '{{out_dir}}'
state: absent
- name: 'Create directory for graph files'
tags: [ 'graph' ]
run_once: true
file:
path: '{{out_dir}}'
state: directory
- name: 'Create DOT language description of topology'
tags: [ 'graph' ]
run_once: true
template:
src: '{{connectivity_prefix}}.j2'
dest: '{{out_dir}}/{{connectivity_prefix}}.gv'
- name: 'Render PNG image from DOT language topology description'
tags: [ 'graph' ]
run_once: true
command: >
dot -Tpng -o{{out_dir}}/{{connectivity_prefix}}.png
{{out_dir}}/{{connectivity_prefix}}.gv
# vim:shiftwidth=2:expandtab: