Run in Cisco Cloud IDE

NSO Restconf - DNS Example

This example shows how you can interact with NSO through Restconf APIs using the requests python library.

The example is designed to be executed on the NSO Playground but any local install will work as long as the specific environment variables are set.

To start experimenting with the example you can:

  • Click on the "Cisco Cloud IDE Run it!" button at the top of the README file
  • Click here to open an environment on Cloud IDE.
  • Go to the repo in Cisco Code Exchange and click on "Run in Cloud IDE" on the left side of the repository name.

The example will be automatically cloned on the playground, ready to test.

Once the playground is ready you can skip to the Build the environment part. When you run the make -C $REPO_DIR build command, it will both create netsims and spin up the NSO instance.

What the script does

The script updates the DNS server configuration of a netsim device using the yang model and template created for this example. The outputs of the calls are printed to the terminal.

It performs the following steps:

  • Creates an HTTP session to NSO.
  • Lists the devices present in NSO.
  • Syncs NSO with the netsim devices using sync-from.
  • Adds the DNS server 1.1.1.1 to ex1.
  • Dry-runs adding a DNS server 2.2.2.2 to ex1.
  • Adds the DNS server 2.2.2.2 to ex1.
  • Lists the rollback files present in NSO.
  • Applies the latest rollback file (0).
  • Dry-runs adding a DNS serve 3.3.3.3 to ex1.
  • Adds the DNS server 3.3.3.3 to ex1.

During the execution the script will print the results to the terminal, and displays the existing DNS configuration.

You can edit the main.py on the playground to experiment with ex0 or ex2 or change the flow of the steps.

Take a look at script_output.log to see what the script prints.

How the example is built

This example uses NSO in local install mode, along with three netsim devices (but only ex1 is used) that work with the IOS example NED.

NSO and netsim are configured automatically using a Makefile.

A simple dns-config template is created to experiment with this example. This template is created, configured and compiled automatically by the same Makefile. You can find the template details under the dns-config directory

The only thing you need to do are the steps described below, and then play with the python script.

This example is based on: $NCS_DIR/examples.ncs/development-guide/basic-automation/showcase_rc.py

Jupyter Notebook

For a step-by-step guide, use the README Jupyter Notebook where you can easily see the REST operations, paths and payloads used.

The rest of the instructions below are the same to what you find on the jupyter notebook.

Setup environment variables

if you run this example outside of the playground, make sure $NCS_DIR points to the NSO directory. Adjust NCS_RUN_DIR and $REPO_DIR to your environment.

The Makefile checks all the environment variables are set, any env var missing will cause the Makefile to fail.

export NCS_RUN_DIR=~/nso-lab-rundir
export REPO_DIR=~/src/nso-restconf-dns-example

Source the ncsrc file.

source $NCS_DIR/ncsrc

Install dependencies

The playground already has the requirements installed, run this command if you are using your own environment.

pip install -r $REPO_DIR/requirements.txt

Build the environment

make -C $REPO_DIR build

Everytime you build the environment, netsim & ncs are stopped, then the environment on $NCS_RUN_DIR is deleted. This is useful to start from scratch everytime when experimenting.

Run example

python $REPO_DIR/main.py

Take a look at the output generated by the script and see the flow of actions performed.

Remove example

make -C $REPO_DIR clean

Addendum

Test template manually without python

This is useful when developing to find the Restconf URL or JSON payload you need to use.

ncs_cli -Cu admin
devices sync-from
config
dns-config ex0 dns-server 1.1.1.1
dns-config ex0 dns-server 2.2.2.2
dns-config ex1 dns-server 1.1.1.1
dns-config ex2 dns-server 5.5.5.5

See what NSO will send to the devices. Commit and exit the configuration mode.

commit dry-run
commit and-quit

In NSO verify config was applied on the devices.

show running-config devices device * config ip name-server

More Restconf Examples

To find more restconf examples that come with NSO do:

find $NCS_DIR/examples.ncs/ -type f -name "showcase_rc.py"
View code on GitHub

Code Exchange Community

Get help, share code, and collaborate with other developers in the Code Exchange community.View Community
Disclaimer:
Cisco provides Code Exchange for convenience and informational purposes only, with no support of any kind. This page contains information and links from third-party websites that are governed by their own separate terms. Reference to a project or contributor on this page does not imply any affiliation with or endorsement by Cisco.