This is an example python script to demonstrate programmatically collecting current running config from all devices managed by Cisco DNA Center and storing each device config file in a Git repo.
This allows you to monitor for configuration changes and track changes to over time.
subprocess
.You can access a virtual DNA Center API from the DevNet Sandbox if you don't want to try this against a physical DNA Center deployment.
This is optional
git clone https://github.com/GShuttleworth/Cisco-DNA-Center-Device-Config-To-Git
Open new Git repo folder in terminal
cd Cisco-DNA-Center-Device-Config-To-Git
It is good practice to run Python scripts in a virtual environment.
Create virtual environment
python3 -m venv venv
Open the virtual environment
source venv/bin/activate
Install Python dependencies
pip install -r requirements.txt
The dna_center_to_git.py
Python script has some variables you need to define.
Edit url
, username
and password
to match your device. By default the connection details are for the DevNet DNA Center Sandbox mentioned above.
You should also set use_ssl_verification
to True
if you are using this code in production to ensure ssl certificates are respected (and used).
# DNA Center Connection Details username = "devnetuser" password = "Cisco123!" url = "https://sandboxdnac.cisco.com" use_ssl_verification = False # Always set to 'True' in production environments
Edit git_repo_url
to match your Git repository URL.
Optional - edit commit_message
to specify what Git should use as the message for each commit.
# Git settings git_repo_url = "https://github.com/your_repo_url" commit_message = "Auto config sync with DNA Center devices"
To run the Python script
python dna_center_to_git.py
The script automatically deletes all created files after the Git push is complete. This means there's nothing to see locally after it has ran.
Go to your remote Git repository and you should see your config uploaded.
If you make changes to any device config and re-run the Python script, you will see Git track the changes between the new config and the old config. Check the changes by viewing the most recent Git commit!
If you have problems running the script, it is likely that the Git installed on your computer hasn't authenticated with the remote Git repository. The Python script relies on connection authentication credentials being cached on your computer.
To fix, manually clone the repository, and if prompted, enter authentication details.
git clone <git_repo_url>
You may need to use an access token rather than a password. Check here for details on creating access tokens if you're using GitHub
Owner
Contributors
Categories
NetworkingProducts
Catalyst CenterProgramming Languages
PythonLicense
Code Exchange Community
Get help, share code, and collaborate with other developers in the Code Exchange community.View Community