Generating anible CSV reports of your Cisco hardware.

For firmware updates, one either has a monitoring tool that offers an overview of the currently used firmware versions on the networking hardware, or one can create a report using ansible to an comma separated value file, known as .csv and then import the data into a spreadsheet program like librecalc or gnumeric or MS Excel. This is only the reporting side of the process. Nothing fancy so far, but gives all the interesting information one would need for decision making on how, if ... etc.

The ansible playbook below will generate a CSV file with basic hardware facts. Actually no more data is needed. This ansible playbook is very basic, and can be used for small enterprise switches much like f.e. the Catalyst 1000, Catalyst 2000 and Catalyst 3000 series. This will not work with Catalyst 4500 and bigger modular switches, only because of the hardcoded flash name in the script. Modular switches mostly have bootflash instead of flash. You would need to rewrite that script for all the other switch series, be it a switch stack or be it a modular chassis switch. Honestly I wanted the script to be as easy as it gets, and did not want to cover all the corner cases. It should be simple and expandable at first. Feel free to take this as a basis for further own expansions.

More it is a simplified and rewritten version of the Brendan Choi gather script who seems to be an networking engineer in Australia. Thanks Brendan.

This script does following:

  • Generate CSV filename using date
  • Put in a header int the output file
  • Get facts from netowrking hardware and write it to a temporary file called csv_tmp
  • Write data into the final export file
  • Remove blank lines from csv
---
# ansible v2.11
- name: Write ios_facts into a csv file
  hosts: c29k
  connection: network_cli
  gather_facts: yes
  vars_files:
    - vault.yml

  vars:
    output_path: "./reports/"
    filename: "device_report_{{ date }}.csv"

  tasks:
  - name: CSV - Generate output filename
    set_fact: date="{{lookup('pipe','date +%Y%m%d')}}"
    run_once: true

  - name: CSV - Create file and set the header
    lineinfile:
      dest: "{{ output_path }}/{{ filename }}"
      line:
        hostname,image,iostype,model,serialnum,system,version,spacefree_kb,spacetotal_kb
      create: yes
      state: present

  - name: CSV - Get IOS devices facts
    set_fact:
      csv_tmp: >
        {{ ansible_net_hostname }},{{ ansible_net_image }},{{ ansible_net_iostype }},{{ ansible_net_model }},{{ ansible_net_serialnum }},{{ ansible_net_system }},{{ ansible_net_version }},{{ ansible_net_filesystems_info['flash:']['spacetotal_kb'] }},{{ ansible_net_filesystems_info['flash:']['spacefree_kb'] }}

  - name: CSV - Write information into .csv file
      lineinfile:
    insertafter: EOF
    dest: "{{ output_path }}/{{ filename }}"
    line: "{{ csv_tmp }}"

  - name: CSV - Blank lines removal
    lineinfile:
      path: "./{{ output_path }}/{{ filename }}"
      state: absent
      regex: '^\s*$'

For further information look up the ansible line in file documentation. This works with ansible version 2.11, check the header of the playbook. Since the ansible development is rather quick, the header makes it easier to identify compatible ansible versions. Much like the cisco version header in the configuration file of your networking gear. Wishing happy automating.