Preview
Table of Contents
netconf get-config is a Yang-based method to get configuration of network devices via netconf protocol.
Besides getting the configuration, the other application of using netconf get-config is to find the appropriate YANG model for each part of the configuration.
netconf get_config Plugins
There are two main Nornir plugins to get the configuration of network devices via Netconf protocol, nornir_scrapli and nornir_netconf.
If we check the source of these two plugins in the Github community, both plugins use the “netconf_get_config” task to get the configuration of network devices.
# "nornir_scrapli" plugin, "netconf_get_config" task def netconf_get_config( task: Task, source: Optional[str] = "running", path: Optional[str] = "", filter_type: Optional[str] = "xpath" ) -> Result: """Get configuration over Netconf from device. Arguments: source (Optional[str]): Configuration datastore to collect from. Defaults to `running` path (Optional[str]): Subtree or xpath to filter. Defaults to `''` filter_type (Optional[str]): Type of filtering to use, 'xpath' or 'subtree'. Defaults to `xpath`
# "nornir_scrapli" plugin, "netconf_get_config" task def netconf_get_config( task: Task, source: str = "running", filter_: Optional[Union[str, List[str]]] = None, filter_type: str = "subtree", ) -> Result: """ Get config from the device with scrapli_netconf Args: task: nornir task object source: configuration source to get; typically one of running|startup|candidate filter_: string of filter(s) to apply to configuration filter_type: type of filter; subtree|xpath Returns: Result: nornir result object with Result.result value set the string result of the get_config operation
In both plugins, the name of the task and the configuration source, which is mostly “running” or running-configuration, are two parameters that we need to specify in the script.
To get a specific part of the configuration and not the entire configuration, we use filter.
Filter type, which can be xpath or subtree, and filter path, which specifies a specific part of a Yang model, are two other optional parameters that can be specified in the script.
Using filters when getting configuration via netconf is discussed in the next sections.
netconf get-config Code Example
Before running the script to get the configuration via netconf, make sure that the nornir, nornir-scrapli, nornir-utils, and nornir-netconf plugins are installed on your automation machine.
python3 -m pip install nornir
python3 -m pip install nornir-scrapli
python3 -m pip install nornir-utils
python3 -m pip install nornir-netconf
This is the script that I have prepared to get the configuration over the netconf protocol.
from nornir import InitNornir from nornir_scrapli.tasks import netconf_get_config #from nornir_netconf.plugins.tasks import netconf_get_config from nornir_utils.plugins.functions import print_result nr = InitNornir(config_file="config.yaml") def netconf_get_config(task): task.run(task=netconf_get_config, source="running") # task.run(task=netconf_get_config, source="running", xmldict="false") results = nr.run(task=netconf_get_config) print_result(results)
As we know, there are two main plugins to get configuration via netconf.
For this reason I included the netconf_get_config task of both nornir_scrapli and nornir_netconf plugins in the script’s header. But one of them is commented and you can run the script once with each plugin to see the result.
In the first script I don’t use any filter and get the entire running-configuration with giving “running” as a configuration source in the parameter.
The output of the netconf protocol is always in XML format. The xmldict option gives us the capability to convert the XML output into a dictionary, which is easy to parse with Python but more difficult to read.
Another point before running the script is that the configuration result from all supported YANG models will be returned in the output of script.
This can be useful to see which YANG model is appropriate for each part of the configuration, which is very useful when we want to change a configuration in both netconf and restconf.
Therefore, running this script is used not only to get the configuration of network devices, but also to find out which is the best YANG model for each part of the configuration.
Now let’s run the script with both plugins and see the result.
Find YANG model for each part of the Configuration
As you can see, the output is very long and difficult to follow.
For the sake of simplicity, I write the output of the script to a file and then grep only the necessary lines.
Here as an example, I want to see all the YANG data models or namespaces used for IP address configuration.
python3 12.1.netconf_get_config.py > output.txt
Since the device’s IP address is 192.168.2.91, I grep the lines containing the IP address and any lines that match the YANG model namespace with the keyword “xmlns“.
[email protected]:~/devnet/pyhton_nornir/2023/12.netconf$ cat output.txt | egrep "192.168.2.91|xmlns" <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101"> <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native"> ... <mld xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-mld"> <address>192.168.2.91</address> ... <interfaces xmlns="http://openconfig.net/yang/interfaces"> <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type> <ipv4 xmlns="http://openconfig.net/yang/interfaces/ip"> <ip>192.168.2.91</ip> <ip>192.168.2.91</ip> ... <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"> <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type> <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"> <ip>192.168.2.91</ip> ...
If we look at the output, you can see that the YANG models “Cisco-IOS-XE-native“, YANG data model “Cisco-IOS-XE-mld” part of “Cisco-IOS-XE- native”, YANG data model “interfaces” from “openconfig” and YANG data model “ietf-interfaces” are used to configure IP addresses of interfaces.
To find the exact YANG file used for each part of the configuration, you can search the name of the XML namespace extracted from the script in all files in the “yang” folder using the “grep -lr” command.
“yang” folder, contains all the yang data models downloaded from the GitHub community in the previous sections.
[email protected]:~/devnet/pyhton_nornir/2023/12.netconf$ grep -lr http://cisco.com/ns/yang/Cisco-IOS-XE-native ~/yang/vendor/cisco/xe/1791/ | grep Cisco
/home/majid/yang/vendor/cisco/xe/1791/YANG_1.1/Cisco-IOS-XE-native.yang
/home/majid/yang/vendor/cisco/xe/1791/Cisco-IOS-XE-native.yang
!
!
[email protected]:~/devnet/pyhton_nornir/2023/12.netconf$ grep -lr http://openconfig.net/yang/interfaces ~/yang/vendor/cisco/xe/1791/ | grep openconfig
/home/majid/yang/vendor/cisco/xe/1791/openconfig-interfaces.yang
/home/majid/yang/vendor/cisco/xe/1791/openconfig-interfaces.yang
...
!
!
[email protected]:~/devnet/pyhton_nornir/2023/12.netconf$ grep -lr urn:ietf:params:xml:ns:yang:ietf-interfaces ~/yang/standard/ietf/RFC/
/home/majid/yang/standard/ietf/RFC/[email protected]
/home/majid/yang/standard/ietf/RFC/[email protected]2014-05-08.yang
Find YANG Model for OSPF Configuration
Using exactly the same method, we can find the YANG model used for OSPF and BGP configuration.
If we look at the OSPF configuration directly in the router, we can see that “0.255.255.255” is used in the OSPF configuration and it can be used as a search keyword in the script output.
R1#show runn | sec router ospf
router ospf 1
router-id 1.2.3.4
network 10.0.0.0 0.255.255.255 area 0
So I use grep to show only the lines of the script’s output including the keywords “0.255.255.255” and “xmlns”.
[email protected]:~/devnet/pyhton_nornir/2023/12.netconf$ cat output.txt | egrep "0.255.255.255|xmlns" <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101"> <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native"> ... <router-ospf xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ospf"> <wildcard>0.255.255.255</wildcard> ...
We see that the “Cisco-IOS-XE-native” YANG model and the “Cisco-IOS-XE-ospf” part of “Cisco-IOS-XE-native” are used to configure OSPF in the IOS XE router.
Now we can search the name of XML namespace in all files in the yang folder using “grep -lr” to find the exact YANG file.
[email protected]:~/devnet/pyhton_nornir/2023/12.netconf$ grep -lr http://cisco.com/ns/yang/Cisco-IOS-XE-ospf ~/yang/vendor/cisco/xe/1791/ | grep Cisco
...
/home/majid/yang/vendor/cisco/xe/1791/YANG_1.1/Cisco-IOS-XE-ospf.yang
...
/home/majid/yang/vendor/cisco/xe/1791/Cisco-IOS-XE-ospf.yang
Find YANG Model for BGP Configuration
With the same method we can find the YANG model used for BGP configuration.
If we look at the configuration of BGP directly in the router, we can see that “65500” is used in the BGP configuration and it can be used as a search keyword in the script output.
router bgp 65500
bgp router-id 5.6.7.8
bgp log-neighbor-changes
So I use grep to show only the lines of the script’s output including the keywords “65500” and “xmlns”.
[email protected]:~/devnet/pyhton_nornir/2023/12.netconf$ cat output.txt | egrep "65500|xmlns" <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101"> <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native"> ... <bgp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-bgp"> <id>65500</id> ... <network-instances xmlns="http://openconfig.net/yang/network-instance"> <type xmlns:oc-ni-types="http://openconfig.net/yang/network-instance-types">oc-ni-types:DEFAULT_INSTANCE</type> ... <name>65500</name> <identifier xmlns:oc-pol-types="http://openconfig.net/yang/policy-types">oc-pol-types:BGP</identifier> <name>65500</name> <as>65500</as> ...
We see that the “Cisco-IOS-XE-native” YANG model and the “Cisco-IOS-XE-bgp” part of “Cisco-IOS-XE-native” are used to configure BGP in the IOS XE router.
In addition, the YANG data model “network-instance” from “openconfig” can also be used for BGP configuration in the IOS XE router.
Now we can search the name of XML namespace, “network-instance” in all files in the yang folder using “grep -lr” to find the exact YANG file.
[email protected]:~/devnet/pyhton_nornir/2023/12.netconf$ grep -lr http://openconfig.net/yang/network-instance ~/yang/vendor/cisco/xe/1791/ | grep openconfig
...
/home/majid/yang/vendor/cisco/xe/1791/openconfig-network-instance.yang
...
To summarize this section, we have used the netconf_get_config task from both nornir_scrapli and nornir_netconf plugins, not only to get the running configuration of network devices but also as a way to find appropriate YANG data model which can be used for each part of the configuration.
This can be useful both in netconf and restconf to edit the configuration which will be discussed in the next sections.
you can download the script itself from this link.