Create An Inventory Excel From A Cisco IOS DeviceΒΆ

Simple implementation for a single host:

The task code, written in Python, consists of a single line. Since the task code is encapsulated within a function, it can and should return data, which will then be passed to the output plugin.

# just return the data generated by cisco.ios.query, as this uses dependency injection,
# jinjamator will automatically ask for required inputs
return cisco.ios.query("show inventory")

All default values for the task can be defined in the defaults.yaml file and overridden using -m <key>:<value>.

output_plugin: excel
# use custom col order and do not show vid col
column_order: "pid:sn:name:descr"
# use nice col headers
rename_columns:
  - pid:PID
  - sn:Serial
  - name:Name
  - descr:Description
# use a proper sheetname instead of sheet 1
sheet_name: Inventory
excel_file_name: demo1

More advanced implementation for multiple hosts:

# just return the data generated by cisco.ios.query, as this uses dependency
# injection, jinjamator will automatically ask for required inputs
return_value = []
# device_list is defined in defaults.yaml. All data defined in
# defaults.yaml will automatically be injected,
# if not defined via -m on CLI or in a site configuration (deamon mode)
for hostname in device_list:
    # as ssh_host is variable we define it via a kwarg, so
    # jinjamator dependency injection just asks for ssh_username
    # and ssh_password if not defined in a site or via -m commandline
    # mapping
    result = cisco.ios.query("show inventory", ssh_host=hostname)
    for line in result:
        line["hostname"] = hostname
    return_value += result
return return_value
# default to excel output plugin
output_plugin: excel
# use custom col order and do not show vid col
column_order: "hostname:pid:sn:name:descr"
# use nice col headers
rename_columns:
  - pid:PID
  - sn:Serial
  - name:Name
  - descr:Description
# use a proper sheetname instead of sheet 1
sheet_name: Inventory
excel_file_name: demo1
device_list:
  - 100.76.0.1
  - 192.168.76.1

Even more advanced implementation for multiple hosts with enhanced GUI:

# as a jinjamator python tasklet is pure python you can always import any external
# lib or function 
from pprint import pformat
from datetime import datetime

return_value = []
ts=datetime.now().strftime("%Y-%m-%d_%H-%M-%S")

# lets manipulate the generated excel_file_name defined in the defaults by adding a timestamp
self.configuration['excel_file_name']=f"{self.configuration['excel_file_name']}_{ts}"

# device_list is defined in defaults.yaml. All data defined in
# defaults.yaml will automatically be injected,
# if not defined via -m on CLI or in a site configuration (deamon mode)
for hostname in device_list:    
    try:
        # as ssh_host is variable we define it via a kwarg, so
        # jinjamator dependency injection just asks for ssh_username
        # and ssh_password if not defined in a site or via -m commandline
        # mapping. Also lower netmiko connection timeout to 1s 
        result = cisco.ios.query("show inventory", ssh_host=hostname, conn_timeout=1)
        for line in result:
            line["hostname"] = hostname
            log.debug(f"got row: {pformat(line)}")
    except Exception as e:
        log.warning(e)
        result=None
    if result:
        return_value += result
# just return the data generated
return return_value
# use single step wizard instead of 3 step wizard
wizard_template: simple
# Show the "are you sure?" Modal before submitting the task
wizard_ask_before_submit: True

# default to excel output plugin
output_plugin: excel
# use custom col order and do not show vid col
column_order: "hostname:pid:sn:name:descr"
# use nice col headers
rename_columns:
  - pid:PID
  - sn:Serial
  - name:Name
  - descr:Description
# use a proper sheetname instead of sheet 1
sheet_name: Inventory
excel_file_name: demo1

device_list:
  - 100.76.0.1
  - 192.168.76.1

all_devices:
  - 100.76.0.1
  - 192.168.76.1
# The defaults.yaml has a limited set of jinjamator tasklet funtions. 
# So you can use a limited set of jinjamator content plugins and variables 
# aswell as all standard jinja2 syntax to generate dynamic defaults.
{%for i in range(1,10)%}
  - 1.1.1.{{i}}
{%endfor%}

# set netmiko to verbose
ssh_verbose: True
# this configutation is based on alpacajs see http://www.alpacajs.org/documentation.html
# the main difference is that the schema and options parts are grouped below the variable
# definition for practicality. Furthermore the form_step 1, 2 or 3 for the wizard can be
# set. The schema.yaml is also a j2 jinamator tasklet. So you can use jinjamator content
# plugins and variables aswell as all standard jinja2 syntax 


device_list:
  form_step: 1
  schema:
    type: array
    enum: 
# generate dropdown values from device_list defined in defaults.yaml
{% for i in all_devices%}
      - {{i}}
{%endfor%}
  options:
    order: 1
    type: select
    multiselect:
      enableFiltering: True
      includeSelectAllOption: True
      enableCaseInsensitiveFiltering: True
    multiple: True
    label: Devices
    helpers:
        - 'Select Switches For Inventory'

ssh_username:
  form_step: 1
  schema:
    type: string
  options:
    order: 2
    label: Username
    helpers:
      - 'Enter SSH Username'

ssh_password:
  form_step: 1
  schema:
    type: string
  options:
    order: 3
    label: Password
    helpers:
      - 'Enter SSH Password'