Ansible Playbook Example to Backup Cisco Router or Switch Config

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to this video tutorial on how to use ansible to backup a Cisco device my name is Roger Perkin I am CCIE number fifty thousand and thirty eight and in the next 15 minutes you are going to learn exactly how to setup ansible to backup the configuration files of all your cisco devices so what i'm going to do is step you through what i have and how we're going to do it so what we are going to be backing up our three csr reuters now I've got them here running on my lab we've got CSR one and that's one ninety one six eight 1.2 10 CSR two is 211 and CSR three is 1.2 one two so these devices are active and running and then I've got my ansible setup now I'm using Visio s-- Visual Studio code here so I'm just going to show you the hosts file so in my host file I have defined three Reuters CSR Reuters so this is my group the CSR - Reuters and I've put in their CSR one two and three and I've used the ansible host underscore host command and put the IP address of each device so this is what we're going to be targeting and then my backup file so this is my backup playbook now I have got this saved in my github and there will be a link in the description of the video so you can just go straight there and clone it but we're gonna walk through it really slowly so I have a block of code here that gets the answerable date and time from the 8th and support date and time facts and it stores that as a fact called DTG and then I use that DTG element in everything I do from this point on so we create a directory so this play but we'll pull the date and time it will then create a directory with the current date I don't actually use the time for my backups I'm doing this sort of once a day so the the date is sufficient for me this will create a directory then so that's here so the path goes in my network programmability backups folder and then the actual playbook that is gonna do the backing up the plays here so I'm targeting CSR Reuters I'm not gonna gather facts and then we're gonna use the iOS command module and we're gonna do show run on the device so the command I'm gonna run is this one show run and then I'm gonna register all the output as this line here config then we're gonna save that into the folder so the next task is to copy the content of config dot STD out and the destination for that is going to be my network programmability folder backups and then the the code here will be the hostname of the device and then the date of the backup there also within this playbook we are going to take some more information and we're gonna run a command here and we can decide which ones we'd like to run so at the moment all of these are commented out and I'm just gonna run show IP interface brief but if you want to you could uncomment these and then run any combination of these commands or add your own in it's great just to get some random commands that you need so if you wanted to find show IP ospf neighbor only advices you'd run these commands uncomment this one here and then that would become an active run so this is gonna run and pull these commands and it's gonna take all the output of those commands and it's gonna put them into a file and I'm gonna use a ginger template to put them into a nice format and then also save them in the backups folder now if you don't want to do any of this you could literally delete everything from this point down and we could just run the backup which would just do the config but I think this is really nice when you're doing a config just to run some show commands as well and I'll show you as well down here the template is this one here so this is also in the github and all this is gonna do is say output from show commands for and then it will put the the route to name in there and then it will loop through the commands and it will just put the output and save them in the file so that's what it's going to do so here we are on the command line so I'm just gonna run the PlayBook and support - playbook and that one's going to be backup dot yml go off first and it's gonna create a folder for me for today's date now I haven't run this today so we should get a new folder which it does it shows it in orange so that says I'm gonna changed we've got a local host change there and it's created me a directory called 20 1902 27 that one there so that folder has now been created on my ansible host I'm now doing a show run on all the cisco csr reuters and it's going to step through these one at a time and then we're going to save the output of that config into this folder Network program is he backups there that's all changed now we're gonna loop around again and we're gonna run the show commands so this is just gonna run sharp interface brief on those it's gonna put the outputs together into one nice text file and it does that and it completed so that's worked so now if I go back into my studio code and we go into the backups folder you this backups folder sorry and we're gonna get into 2019 and here's today's 20 1902 27 you can see I've now got six files in here we have CSR one and then it's appended the year the month and the date and it's got a config and we click on that file and this now has got the configuration file for that router so that one was yes our one which was 1 & 2 1 6 8 1.2 10 so I've got that out but and also we've got these other files which are the shotput and this is just run the show IP interface brief command and there we go we've confirmed that that is the IP address so there is a very simple playbook to back up Cisco devices now this can be adopted for Nexus just change the modules it can be adopted for a SAS again just change the module and if I'll just step through a couple things in here so what it's doing is very simple this bit was just off the end here but we run the show run and then we register that to config so this word can be here can be anything you like as we've got it set as config just for clarity and then when we save the file we are going to copy it and the content is going to be config dot STD at 0 and we're gonna put it into this folder network we're gonna take backups and what we didn't show all the way at the end was the inventory hostname - date - config so this is what it's doing is it's saving it to BCS r1 - date - config text and that is the flower we've got now if we run this again with ants will be an idempotent nothing has changed on my Reuters so this time the folder is already created so it doesn't need to do that so that's green we don't need to make the change on the folder the show run is gonna run in green anyway and know the output so we're saving the output and that's green as well because nothing has changed so the backup file we got the first time ran is the same as the one we've just got now so to prove that and to show what you can do with ansible that one's there we make a change onto one of these Reuters so let's just connect into Roger at a tune-up 1 6 8.1 not to 10 so this is CSR one so now I'm CSR one if I make a change on this router so I've now made a change on this router so this isn't what you would say an unauthorized change it's not been made by ansible it's been made by someone else outside of the system so when we run our backup this will notice that a change has been made on route 1 so what should happen now is that when the show run out / is saved for CSR 1 it would show up as orange so it successfully performed the show run but now is saving the output it's gonna be different there it is to the other one so that's highlighted that something has changed so if you're using a network automation system to backup all your devices and you're the only one that's doing it or only one system is doing it and this runs this can be flagged as there was an unauthorized change being been made there oh we can see we've got all my backup folders in here 20:19 - oh - - 27 there's all the outputs now these are not version controlled as such in every single changes being made so this is just taking a daily backup so every backup that gets run today in this system will just overwrite the current file and in tomorrow it will create a new folder and the new backups will be done so it's not fully a version control system but it's a great way just to get some snapshots of your backups and I use this an awful lot when I'm with clients and just to give me a point in time where I can just point out all the devices into the house phone run this grab all the convicts and grab some load of show outputs and we can review that at a later date so I hope this has been of use to somebody as I said the github link to the playbook is in the description of the video if you've enjoyed this then please subscribe just hit the subscribe button and you'll get notified of any new videos I do I also have an answer ball automation course the link for that is also in the description and I look forward to seeing you in the next video thank you
Info
Channel: Roger Perkin
Views: 14,335
Rating: 5 out of 5
Keywords: ansible cisco, ansible network automation, ansible for network engineers, ansible playbook, ansible, network automation, roger perkin, network automation with ansible, ansible network automation tutorial, ansible playbook tutorial, ansible network automation use cases, ansible network playbook example, ansible training videos, ansible playbook cisco example, how to backup cisco configuration using ansible, ansible playbook example, ansible backup cisco config
Id: w8Sf3rXncs0
Channel Id: undefined
Length: 13min 26sec (806 seconds)
Published: Wed Feb 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.