0

I've got an Ansible playbook that pulls interface descriptions from two routers and writes the results to a CSV file. When it iterates through the interfaces it writes one interface per router to the file

---
- name: Cisco get ints
  hosts: test
  gather_facts: false
  connection: local
  become: false

  vars:

    csv_path: /tmp
    csv_filename: int_audit.csv
    headers: Hostname,Interface,Description

  tasks:

    - name: Save CSV headers
      ansible.builtin.lineinfile:
        dest: "{{ csv_path }}/{{ csv_filename }}"
        line: "{{ headers }}"
        create: true
        state: present
      delegate_to: localhost
      run_once: true

    - name: run show inventory on remote device
      iosxr_facts:
        gather_subset: interfaces
      register: output

    - name: Write int desc to csv file
      loop: "{{ output.ansible_facts.ansible_net_interfaces | dict2items }}"
      lineinfile:
        dest: "{{ csv_path }}/{{ csv_filename }}"
        line: "{{ output.ansible_facts.ansible_net_hostname }},{{ item.key }},{{ item.value.description }}"
        create: true
        state: present
      delegate_to: localhost

so I end up with a list that has no order.

$ cat /tmp/int_audit.csv

Hostname,Interface,Description
RTR1.LAB1,BVI13,LOCAL:RTR2.LAB1:[L3]
RTR1.LAB1,Bundle-Ether1100.128,LOCAL:RTR2.LAB1:BUNDLE1100:20GE[UTIL]
RTR2.LAB1,Bundle-Ether1100.128,LOCAL:RTR1.LAB1:BUNDLE1100:20GE[UTIL]
RTR1.LAB1,Loopback0,LOOP:LOOP0-RTR1.LAB1:[N/A]
RTR2.LAB1,Loopback0,LOOP:LOOP0-RTR2.LAB1:[N\A]

I'd like to have it sort the list by router name.

Any help is appreciated.

U880D
  • 8,601
  • 6
  • 24
  • 40
  • 2
    There is a `sort` filter available that may help out, but the way you've structured your playbook this only makes sense if you are clearing and re-writing all the entries for a given value of `output.ansible_facts.ansible_net_hostname`. There's no way to sort *existing* entries in the file. – larsks Jan 18 '23 at 21:34
  • Thanks - I may end up having to write a perl script to clean up the csv file. – William McGuire Jan 18 '23 at 22:25
  • Unfortunately there is no information or description about what you try to achieve at the end, why do you need the file sorted, where and how do you like to proceed further with the file. Therefore I've provided a hint regarding the Linux `sort` command with which you can simple, fast and cheap get a sorted CSV file on the Control Node by post-processing. – U880D Jan 20 '23 at 08:59

1 Answers1

0

You could in example achieve you goal by simply post-processing the file on the Control Node.

For the test file

cat test.csv
Hostname,Interface,Description
RTR1.LAB1,BVI13,LOCAL:RTR2.LAB1:[L3]
RTR1.LAB1,Bundle-Ether1100.128,LOCAL:RTR2.LAB1:BUNDLE1100:20GE[UTIL]
RTR2.LAB1,Bundle-Ether1100.128,LOCAL:RTR1.LAB1:BUNDLE1100:20GE[UTIL]
RTR1.LAB1,Loopback0,LOOP:LOOP0-RTR1.LAB1:[N/A]
RTR2.LAB1,Loopback0,LOOP:LOOP0-RTR2.LAB1:[N\A]

the sort command will result into

sort -k1 -n -t, test.csv
Hostname,Interface,Description
RTR1.LAB1,Bundle-Ether1100.128,LOCAL:RTR2.LAB1:BUNDLE1100:20GE[UTIL]
RTR1.LAB1,BVI13,LOCAL:RTR2.LAB1:[L3]
RTR1.LAB1,Loopback0,LOOP:LOOP0-RTR1.LAB1:[N/A]
RTR2.LAB1,Bundle-Ether1100.128,LOCAL:RTR1.LAB1:BUNDLE1100:20GE[UTIL]
RTR2.LAB1,Loopback0,LOOP:LOOP0-RTR2.LAB1:[N\A]

Similar Q&A

and more

U880D
  • 8,601
  • 6
  • 24
  • 40