cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
525
Views
1
Helpful
4
Replies

Cisco Catalyst 9300 Wrap CLI in XML for NETCONF Transport

caesarkrit
Level 1
Level 1

Hi,

My current Cisco Lab environment details are as follows:
Cisco IOS XE Software, Version 17.12.04
Cisco IOS Software [Dublin], Catalyst L3 Switch Software (CAT9K_IOSXE)

My current Lab structure is as follows:

I use a linux system that runs Ansible in a virtual Env.

I have a YAML files called ntp.yml

# Description: Defines NTP servers for time synchronization
---
ntp:
  servers:
    - server: 10.0.0.10        # Primary NTP server
    - server: 10.0.0.12        # Secondary NTP server
    - server: time.stjude.org  # Tertiary NTP server
      vrf: Mgmt-vrf           # Using management VRF
 
I have XML that reads the configuration data:
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<ntp operation="replace">
<server xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ntp">
{% if ntp.servers is defined %}
{% for server in ntp.servers %}
{% if server.vrf is defined %}
<vrf>
<name>{{ server.vrf }}</name>
<server-list>
<ip-address>{{ server.server }}</ip-address>
</server-list>
</vrf>
{% else %}
<server-list>
<ip-address>{{ server.server }}</ip-address>
</server-list>
{% endif %}
{% endfor %}
{% endif %}
</server>
</ntp>
</native>
</config>


I then have a task file in my ansible role that is able to read the configuration and send it via NETCONF to the switch to modify the configuration, it all works great. The problem I have found is that writing XML for each block of configurations(NTP, SNMP, Spanning_tree, VTY, Etc) is not really scalable for me or the network engineers. I was looking at wrapping CLI commands in the XML instead of going pure XML which will make things easier. I have been trying to figure this out for a while and cant seem to get a basic example going to being able to convert my current XML to wrap in CLI. Does anyone have experience in doing this? is it possible? do you suggest a specific tutorial or piece of documentation to do this? and is it well supported? just wanted to get the ins/outs of this and if it might be able to replace my long list of XML writings. Feel free to ask for code files or project structure.
 
Thanks
1 Accepted Solution

Accepted Solutions

Hey @caesarkrit yangtoxml can convert your YANG models to XML templates, this is part of pyang toolkit and can help in generate initial XML structure. 

Agree it’s a common issue to find balance between machine readability and human readability.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

View solution in original post

4 Replies 4

@caesarkrit imo wrapping the cli commands within NETCONF is not recommended, this goes against the intended design and benefits of NETCONF. I would look more at Jinja2 Templates for XML Generation and create the reusable template blocks that you can import and include in other templates. 

This or instead of writing large XML blocks, consider using modular YANG models. 

Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Hi,

I agree that this isn't best practices or something recommended, so I'll provide some background on why I would want to do this.

What's happening:
1. I store desired configurations in YAML files, in this case the first image ntp.yml
2. Jinja2 templates convert YAML into XML structure, in this case the second image ntp.j2
3. XML is sent via NETCONF to configure device.
4. Device validates against its YANG models.
"YAML (human configs) -> Jinja2 Templates -> XML -> NETCONF -> Device".

My idea and goal is to move away from the complex jinja2 templates for XML. It would be great if there was a tool that converts switch configurations into XML or switch configuration into the jinja2 templates. The reason I am looking for this is to allow others to be able to modify the configuration they want (in YAML) and be able to adjust the jinja2 template if they need be.

caesarkrit_2-1735653913651.png

caesarkrit_1-1735653842106.png

 

Hey @caesarkrit yangtoxml can convert your YANG models to XML templates, this is part of pyang toolkit and can help in generate initial XML structure. 

Agree it’s a common issue to find balance between machine readability and human readability.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

I found the below, Thanks. I'll look into it and see how I can leverage this in my project to allow others to quickly create the XML files.

Much appreciated

https://github.com/vhosakot/yang_to_xml