05-13-2024 01:01 AM
I'm just automating my company's network using Ansible and Netbox for dynamic inventory and Source of Truth (SoT). I have a general question: how do you deal with old, unsupported IOS devices when working with Ansible? Some modules require commands like "show run | section," which my Cisco 4506 switches can't handle. Right now, I'm using a TTP template within a rescue block to format the output from "show run" on these unsupported IOS devices.
Solved! Go to Solution.
05-13-2024 02:24 AM
While TTP templates can be helpful, Jinja2 templates offer more power and flexibility. You can use Jinja2 templates within your playbooks to process the output of "show run" on unsupported devices. You can manipulate the output of "show run" on unsupported devices. You can customize this template further to extract specific data or format the output according to your needs and use case. Hope this helps.
---
- hosts: cisco_ios_old
tasks:
- name: Get show run output
ios_command:
command: show run
register: show_run_output
- name: Process show run output with Jinja2 template
template:
src: show_run.j2
dest: /tmp/processed_show_run.txt
register: processed_output
- name: Debug processed output (optional)
debug:
msg: "{{ processed_output.stdout }}"
# Extract the configuration for interfaces starting with 'Fa0/'
{% set interfaces = show_run_output.stdout.split('\n') %}
{% set fa_interfaces = [] %}
{% for line in interfaces %}
{% if line.startswith('interface Fa0/') %}
{{ line }}{% set fa_interfaces.append(line) %}
{% endif %}
{% endfor %}
# Filter out unwanted lines and format the output
interface Fa0/0
description: Connected to Router1
switchport mode access
switchport access vlan 10
interface Fa0/1
description: Connected to ServerA
switchport mode access
switchport access vlan 20
{% for intf in fa_interfaces %}
{{ intf }}{{ '\n' }}
{% endfor %}
05-13-2024 02:24 AM
While TTP templates can be helpful, Jinja2 templates offer more power and flexibility. You can use Jinja2 templates within your playbooks to process the output of "show run" on unsupported devices. You can manipulate the output of "show run" on unsupported devices. You can customize this template further to extract specific data or format the output according to your needs and use case. Hope this helps.
---
- hosts: cisco_ios_old
tasks:
- name: Get show run output
ios_command:
command: show run
register: show_run_output
- name: Process show run output with Jinja2 template
template:
src: show_run.j2
dest: /tmp/processed_show_run.txt
register: processed_output
- name: Debug processed output (optional)
debug:
msg: "{{ processed_output.stdout }}"
# Extract the configuration for interfaces starting with 'Fa0/'
{% set interfaces = show_run_output.stdout.split('\n') %}
{% set fa_interfaces = [] %}
{% for line in interfaces %}
{% if line.startswith('interface Fa0/') %}
{{ line }}{% set fa_interfaces.append(line) %}
{% endif %}
{% endfor %}
# Filter out unwanted lines and format the output
interface Fa0/0
description: Connected to Router1
switchport mode access
switchport access vlan 10
interface Fa0/1
description: Connected to ServerA
switchport mode access
switchport access vlan 20
{% for intf in fa_interfaces %}
{{ intf }}{{ '\n' }}
{% endfor %}
05-14-2024 01:06 AM
Use Netmiko to cover that gap on old devices and use TextFSM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide