I am trying to run a show command on a cisco router to find out the down interfaces.
I would like to print only the Ethernet Interface name(for eg:Eth1/3) for those are having the Status as down in the command output in a JSON fromat.
Cisco output(input.json)
[
[
[
"--------------------------------------------------------------------------------",
"Ethernet VLAN Type Mode Status Reason Speed Port",
"Interface Ch #",
"--------------------------------------------------------------------------------",
"Eth1/20 -- eth routed down XCVR not inserted auto(D) --"
]
],
[
[
"--------------------------------------------------------------------------------",
"Ethernet VLAN Type Mode Status Reason Speed Port",
"Interface Ch #",
"--------------------------------------------------------------------------------",
"Eth1/21 -- eth routed down XCVR not inserted auto(D) --"
]
],
[
[
"--------------------------------------------------------------------------------",
"Ethernet VLAN Type Mode Status Reason Speed Port",
"Interface Ch #",
"--------------------------------------------------------------------------------",
"Eth1/22 -- eth routed down XCVR not inserted auto(D) --"
]
],
[
[
"--------------------------------------------------------------------------------",
"Ethernet VLAN Type Mode Status Reason Speed Port",
"Interface Ch #",
"--------------------------------------------------------------------------------",
"Eth1/23 -- eth routed down XCVR not inserted auto(D) --"
]
]
]
Playbook
- name: Set global device name
set_fact:
global_device_name: "{{ device_name }}"
- name: Execute command on interfaces
cisco.ios.ios_command:
commands:
- "show interface {{ item }} brief"
loop: "{{ hostvars[global_device_name]['ports'] }}"
delegate_to: "{{ global_device_name }}"
register: command_output
changed_when: false
- name: Create a list of interface details
set_fact:
interface_list: "{{ interface_list|default([]) + [item.stdout_lines] }}"
loop: "{{ command_output.results }}"
loop_control:
label: "{{ item.item }}"
- name: Save output to JSON file
copy:
content: "{{ interface_list | to_nice_json}}"
dest: output.json
- name: Firewall Rule Create/Update variable
set_fact:
command_output: "{{ lookup('file', 'output.json') }}"
- name: Firewall Rule Create/Update variable
set_fact:
interface_details: "{{ command_output|map('flatten')}}"
- name: Format output
debug:
msg: "{{ interface_details }}"
Here the interface_details is the Cisco output
With the above playbook i am getting an empty array.