menu

GIT and Python to manage DNA-C templates

I have written about automation with DNA-C before, and how it becomes really efficient once you use some external scripting. Sure you can get a lot out of the web GUI, but you are still locked in a situation where you have to act human middleware. This post is about how to utilize tools such as Python and GIT to acheive both time and quality.

Regardless if you are one person working with different DNA-C installations, or many people working with one, you have an operational model that demands some thinking – what is our source of truth?

Let’s say you want to syncronize settings, like templates, between DNA-C appliances. Perhaps you want to copy your templates from your LAB environment and run the very same templates in production. It is very likely that you make some mistakes copying them by hand.

Template management

Many times have I heard complaints about DNA-Centers lack of synchronizing data between appliances. I think that’s wrong, you can do that very easy using a little piece of Python code. You are not looking for another appliance to sync your settings – you are looking for a modern work methodology.

Let’s write some Python

Just to prove how easy it is, I wrote some extensions to my module ezdnac (easy dna-c) to manage all template sync. I wrote one function called pullTemplates, and one called pushTemplates. Let’s look at how you can use them!

The above script will connect and authorize against the given DNA-C, then download all of the templates to the folder ‘templatesFolder’. Note – You could also specify a given project if you don’t want to download all templates.

When we run the above script, it will generate one file per template:

Now that we have all templates downloaded, we could upload them to our three production DNA-Centers, let’s again utilize the ezdnac module:

Once this script ended, we will have the exact same templates in all our production DNA-Centers. And if any template was found previously with the same project and name, it will be updated according to your last push, which brings us directly to the next part.

DNA-C Templates in GIT and CI/CD workflow

Being able to synchronize templates between DNA appliances is handy. But if you really want to start leverage a devnet approach, you could really reap some nice results!

Let’s say you created your first versions of all templates you need. You pulled them down to your management server or personal laptop. Next step – you push them to your GIT repository! Because looking at the templates, working with the JSON structure could be almost as easy as writing them in the webgui, at least when doing changes.

Or you do the updates from the LAB-DNA and once tested you extract the newer version and push it to GIT. Once GIT is updated it triggers a job in your CI/CD pipeline that collects the new templates from GIT, and pushes them to all of your DNA-Centers! Now we are finally talking automation!

Leave a Reply

Your email address will not be published. Required fields are marked *