Skip to content

Commit

Permalink
Configuring loopback from Jinja2
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers committed Aug 4, 2022
1 parent 40b7bca commit b4040b6
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
56 changes: 56 additions & 0 deletions netconf/iosxe_candidate_p2/cfg_loopback_j2/cfg_loopback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os
from lxml import etree # noqa
from ncclient import manager
from rich import print
import pdbr # noqa

from jinja2 import FileSystemLoader, StrictUndefined
from jinja2.environment import Environment


def generate_loopback_cfg(j2_vars=None):

if j2_vars is None:
j2_vars = {}

j2_env = Environment(undefined=StrictUndefined, keep_trailing_newline=True)
j2_env.loader = FileSystemLoader(["."])

template_file = "loopback_cfg.j2"
template = j2_env.get_template(template_file)
return template.render(**j2_vars)


def save_xml(filename, xml_content):
with open(filename, "w") as f:
f.write(xml_content)


if __name__ == "__main__":

device = {
"host": "3.85.14.166",
"port": 830,
"username": "pyclass",
"password": "bogus",
"hostkey_verify": False,
}
device["password"] = os.environ["PYNET_PASSWORD"]

with manager.connect(**device) as m:

# Generate XML
loopback_vars = {"loopback_number": "1"}
xml_content = generate_loopback_cfg(loopback_vars)
add_loopback = xml_content

# Save out to a file (so we have a record of the config)
save_xml("loopback_cfg.xml", xml_content)

# Stage the new loopback configuration on the candidate config.
nc_reply = m.edit_config(target="candidate", config=add_loopback)
print(f"\n{nc_reply}\n")

# Commit the candidate configuration to running config.
nc_reply = m.commit()
print(f"\n{nc_reply}\n")
42 changes: 42 additions & 0 deletions netconf/iosxe_candidate_p2/cfg_loopback_j2/loopback_cfg.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<interface>
<Loopback>
<name>{{ loopback_number }}</name>
</Loopback>
</interface>
</native>
<interfaces xmlns="http://openconfig.net/yang/interfaces">
<interface>
<name>Loopback{{ loopback_number }}</name>
<config>
<name>Loopback{{ loopback_number }}</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:softwareLoopback</type>
<enabled>true</enabled>
</config>
<subinterfaces>
<subinterface>
<index>0</index>
<config>
<index>0</index>
<enabled>true</enabled>
</config>
<ipv6 xmlns="http://openconfig.net/yang/interfaces/ip">
<config>
<enabled>false</enabled>
</config>
</ipv6>
</subinterface>
</subinterfaces>
</interface>
</interfaces>
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface>
<name>Loopback{{ loopback_number }}</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:softwareLoopback</type>
<enabled>true</enabled>
<ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
<ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
</interface>
</interfaces>
</config>
42 changes: 42 additions & 0 deletions netconf/iosxe_candidate_p2/cfg_loopback_j2/loopback_cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<interface>
<Loopback>
<name>1</name>
</Loopback>
</interface>
</native>
<interfaces xmlns="http://openconfig.net/yang/interfaces">
<interface>
<name>Loopback1</name>
<config>
<name>Loopback1</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:softwareLoopback</type>
<enabled>true</enabled>
</config>
<subinterfaces>
<subinterface>
<index>0</index>
<config>
<index>0</index>
<enabled>true</enabled>
</config>
<ipv6 xmlns="http://openconfig.net/yang/interfaces/ip">
<config>
<enabled>false</enabled>
</config>
</ipv6>
</subinterface>
</subinterfaces>
</interface>
</interfaces>
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface>
<name>Loopback1</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:softwareLoopback</type>
<enabled>true</enabled>
<ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
<ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
</interface>
</interfaces>
</config>
42 changes: 42 additions & 0 deletions netconf/iosxe_candidate_p2/cfg_loopback_j2/loopback_cfg.xml.bup
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<interface>
<Loopback>
<name>99</name>
</Loopback>
</interface>
</native>
<interfaces xmlns="http://openconfig.net/yang/interfaces">
<interface>
<name>Loopback99</name>
<config>
<name>Loopback99</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:softwareLoopback</type>
<enabled>true</enabled>
</config>
<subinterfaces>
<subinterface>
<index>0</index>
<config>
<index>0</index>
<enabled>true</enabled>
</config>
<ipv6 xmlns="http://openconfig.net/yang/interfaces/ip">
<config>
<enabled>false</enabled>
</config>
</ipv6>
</subinterface>
</subinterfaces>
</interface>
</interfaces>
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface>
<name>Loopback99</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:softwareLoopback</type>
<enabled>true</enabled>
<ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
<ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
</interface>
</interfaces>
</config>

0 comments on commit b4040b6

Please sign in to comment.