-
Notifications
You must be signed in to change notification settings - Fork 14
/
ios-inventory.yml
executable file
·60 lines (51 loc) · 2.03 KB
/
ios-inventory.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
# Copyright Jason Barbee 2017
# www.TekLinks.com
# jbarbee@teklinks.com
# License MIT
# This depends on modules from Network To Code library. Many thanks to them for a great library.
# https://github.com/networktocode/ntc-ansible
---
- name: Inventory Routers and Switches and Build CSV for MyDevices.csv
hosts: routers
gather_facts: no
connection: local
# this is for the IOS Module provider
vars:
cli:
host: "{{ inventory_hostname }}"
username: "{{ username }}"
password: "{{ password }}"
transport: cli
tasks:
# Collect all facts from the device - this helps set the hostname
- ios_facts:
gather_subset:
- config
provider: "{{ cli }}"
# Network To Code module to grab inventory data
- name: Show Inventory
ntc_show_command:
connection: "{{ connection }}"
platform: "{{ platform }}"
port: "{{ port }}"
command: 'show inventory'
host: "{{ inventory_hostname }}"
username: "{{ username }}"
password: "{{ password }}"
register: results
- debug: var=results.response
- name: Bundle data in a new hash, but only if Serial is not blank.
set_fact:
inventory: "{{ inventory|default([]) + [ {'hostname': ansible_net_hostname, 'ip': inventory_hostname, 'name': item.descr, 'serial': item.sn, 'version': ansible_net_version } ] }}"
with_items: "{{ results.response }}"
when: item.sn != ''
#- name: Debug Check if you want
# debug: var=inventory
# Write Headers for Cisco My Devices - hostname,IP,ProductID,serial
- name: Write mydevices.csv
run_once: true
shell: echo Serial Number,Device Name,Tags,Notes > mydevices.csv
with_items: "{{ inventory }}"
- name: Write My Devices CSV File
shell: 'echo {{ item.serial }},{{ item.hostname }},"IP-"{{ item.ip }} "|" "IOS-{{ item.version }}", >> {{ path_prefix }}mydevices.csv'
with_items: "{{ inventory }}"