0

I am pulling network switches interfaces information e.g. interface name, admin status, description etc. and parsing those values into a single .csv file but it seems like it is overwriting a file rather than appending a file, can you please let me know if I am doing something wrong here ? or there is better way to achieve this, here is my playbook.

---

- name: NXOS interfaces operation status 
  hosts: NXOS
  connection: local
  gather_facts: false

    
  tasks:

    - name: Discover NXOS interfaces 
      nxos_command:
        commands:
          -  show run | grep ignore-case interface | exclude feature | exclude source | exclude destination | exclude logging
      register: interface_names

    - name: Get the interfaces stats 
      nxos_command:
        commands:
          # Full interface operational status 
          # - command: show interface {{ item.split(' ')[1] }}
          # Interface name 
          - command: show interface {{ item.split(' ')[1] }} | head lines 1 |  awk '{ print $1 }'
          # Interface description  
          - command: show interface {{ item.split(' ')[1] }} | grep "Description"
          # Interface operation status: 
          - command: show interface {{ item.split(' ')[1] }} | head lines 1 | awk '{ print $3}'
          # Interface admin-status  
          - command: show interface {{ item.split(' ')[1] }} | grep "admin state" | awk '{ print $4 }'
          # Interface MTU size   
          - command: show interface {{ item.split(' ')[1] }} | grep "MTU" | awk '{ print $2 }'
      # Loop over the interfaces output from the previous task 
      loop: "{{ interface_names.stdout_lines[0] }}"
      register: interfaces_stats

    - name: Saving interfaces facts for NXOS 
      copy:
        content: |
          #jinja2: lstrip_blocks: True, trim_blocks: True
          {{ inventory_hostname }}

          Interface name,Interface description,Interface operational-status,Interface admin-status,Interface MTU size 
          {{ item.stdout_lines[0] }},{{ item.stdout_lines[1] }},{{ item.stdout_lines[2] }},{{ item.stdout_lines[3] }},{{ item.stdout_lines[4] }}

        dest: /opt/ansible/vsansible/info/{{ inventory_hostname }}_info.csv
      with_items:
        - '{{ interfaces_stats.results }}'

here is the current output shows

Interface name,Interface description,Interface operational-status,Interface admin-status,Interface MTU size
[u'mgmt0'],[u'Description: -sw01-Gig0/39'],[u'up'],[u'up,'],1500

However, it should show me the output for every interface I am looking through in my first task.

please advice.

Muhammad Rafi
  • 15
  • 1
  • 6

0 Answers0