Ansible Jinja2 Template Tutorial Network Automation Cisco

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to this ansible tutorial in which we're going to go over using ansible to generate network configuration templates my name is roger Perkin I am CCIE number fifty thousand and thirty eight welcome to the video let's get started okay so this video has been inspired by a three part blog post by buyers link to that is in the description below in which he goes through how to do this it's basically talking about a generic way of configuring Rooter configuration templates based on different configs and template variables let's do this using ansible you need to have everything set out in a certain way and sub always looking for files in certain locations so let's just step through that now so we've got ansible installed here and at the moment i am using a constable version 2.3 now you can use that via the ansible desktop version command so we need to go into my templating folder so I've got a folder which I've created this is called templates now within this we can see the folder structure that you need to have to be able to use ansible to generate your configuration templates now it uses roles with announce table which is a common concept and within the roles we need to generate a folder now in my instance we're using Rooter I've got another one that I've started here which is switch so these are two separate roles and within the Rooter role I've got three other sub folders which are tasks templates and VARs so these are variables in here so to start this with your ansible installation you need to create this folder structure so create a folder called roles and within that folder Ruta folder and then tasks templates and VARs and within those folders we're placing these files so we've got a main llamo file we've got a ginger file called Rooter j2 and a main don't you know file here now I'm going to go into all of these files and show you what they contain and how they all connect together and the overarching file that connects everything together is this site gem or file and this is the playbook that we're going to run to generate the configurations so if just pause the video for a second make a note of this folder structure and if you can go into your ant school and create these folders and these files within those folders then you'll be in a good place to follow along with the tutorial but for now let's just go through these one by one and you can see what's in each file and how they're going to operate so we'll start by the main site gem of how this is the top level play but ok so this is very simple this is the mo file this is the play but that we're going to run and the name just generate root of configuration files it's going to run locally and it's looking for the roles so we've defined one role in this which is the Rooter roles so this is the first element and this element now references this role here so if that said switch it would then look in the folders underneath the switch to make its decisions and what it did from there so we're going to run the PlayBook and it will look into this folder structure Rooter and it's going to first look into the main gamal file as the task that it's going to run so let's drop into that folder now so once we initiate the playbook the site llamo is saying a reference the main llamo in the tasks folder that will be done automatically an supporters knows where to look for that and let's look in here so the main got llamo file hold on Reuters sorry into tasks that's right so this is the tasks folder this is where it's going to reference it okay so this is what's going to happen once you initiate the playbook it's going to reference the roles of Rueter go into the root of subfolders and then come here generate the configuration files is the name and then we're going to use the template module within ansible which is going to say using the source route Agyei - so this is a ginger file which we're going to go into next so it says using the source of that and the destination this is where the configuration files are going to be placed once there when they're done and I've created a folder called configs and they're going to be created with the host name and they'll be txt so the host name of each router will be in the config folder and we're going to be using this file to generate those configurations and it's going to use the items in Reuters so let's go back one step so we look into the Rooter subfolder here and we're going to now say what's this file this is the ginger template so this task is going to reference the source for the template here so we're going to drop into templates this might seem a little bit confusing now but once you've been through it once and I show you exactly what's happening we'll see how logical it all fits together so within this file this is the ginger template so in here we're going to be placing the actual root configuration so this is the standard configuration lines that are going to appear in every single config and then when we reach a item that will have a different variable ie the hostname the loopback address the enable secret password these configuration lines are then replaced with an item so this one is the item hostname item got loopback item dot enable secret so these will be referencing some variables which are defined in the variables folder so let's step into that one and we'll go back now and we'll drop in to the variables folder and these are my variables so I've got and you can see here it's got Reuters and that was referenced earlier on when when it created the configuration files except with items in Reuters so what it's going to do is loop through all of these and generate a configuration file for root of one it's going to generate a configuration file for root of two and one for root of three using these elements in the configuration now these are all the same at the moment this could be different in fact let's just make that difference so you can see otherwise that could just be the same one for each each reader now as you can see this is very basic and the power of ansible is that this can be dynamic this can scale for thousands of devices this demonstration is just a very simple way of just showing you what the building blocks are and once you've mastered the method of this it's just going to scale and get larger and larger so let's just drop out of that and let's just run the PlayBook and we'll see exactly what happens now I will go into the config folder just to show you there's currently nothing in there so that folder configs is empty and I'm going to step through this once more just to show you we're going to initiate this playbook site Gemmell and in that playbook it's said to use the rolls Reuter so the play but will then go into the Rooter role and it will be looking for a playbook in here which we've got and this playbook says I'm going to generate a template a templated configuration and I'm going to use the configuration in here in the ginger template with the variables that are defined here so this is going to merge the standard configuration with the variables from each section so let's just run this and see what happens if you run it by ansible playbook site dot y mo okay now as it's gone orange there you can see that it's changed so the the playbooks gone through it came to this task generate the configuration files which was defined in here in the main channel and all three items changed so if we go back now into the config folder you can see I have three files three text files and if I go into one of these we can cut that one show you the contents of that configuration file here is the standard configuration and then it's then replaced in root of one hostname r1 let's put the IP address of 1 1 1 1 and the enable secret is Roger 1 if we do this for the rooted to config you can see we've now picked root of 2 and the IP address of 2 and the password of 2 so you can see that the two elements the variables file so we'll go back to that and show you we're going to the roles and again we've got the Rooter role so the variables file this is the one that contained the different IP addresses and the different passwords the ginger template is the one that contained the standard configuration with the variables specified on each element that needs to be different between each one and the main gamma was the file that just ran straight away so that's it pretty much it's a very straightforward it's very logical but I suggest you go back to the video again and create this folder structure create these files and then if you want to add to this so this is a very very simple configuration as you can see but if I want to increase this and make it at an item into there so let's go into the Ruta roll and we're going to go down to the template and into the root of j 2 which is the ginger file and we'll add an extra line in so we've got the hostname we've got interface loopback 0 so if I wanted to add an interface in here addressing of an interface so we would just put in here let's go down and I say interface Gigabit Ethernet 0 IP address and as this is going to be different for each configuration we put the curly brackets in it's going to be an item that's referenced in the variable files and I'll just put a gig 0 in there IP so we call it gig 0 IP so the next time we run this it's going to go through the configuration each time it gets to an item here it's going to go into the variables file pick that variable a and replace it in here into the configuration so we've got the gig 0 IP I've now got to create those variables in the variables folder so we go into VARs and edit the main Yama file and these are all the variable elements for each router so in this one I called it gig 0 IP and this is going to be 10.10 dot big 0ip this is going to be 20 dot twenty dot 2020 and this one gig zero IP now you might be thinking at this point that it would be just quicker just to create the config by hand and edit them and do it and yes it would for this scale but when you're dealing with hundreds or thousands of routing configurations and this file is twenty thirty times bigger and there are so many more different elements in there this is where the power ansible comes in and you can for sure have everything exactly configured and provisioned to the letter so you know there's nothing going wrong and then the next step for this would be to deploy those configs to a router so with a like a zero touch deployment project if you had to configure a thousand reuters you can get a base IP onto that box get the box up use ansible to discover it deploy the configuration derive it from the template make the configuration and then push it to the root set or very simply but this example is just to get you know baby steps just to get you started find out how it works and then just build on it as you go so now we've added those elements in we're going to run the PlayBook again [Music] now it's going to go through the plane again and this time it will change so you can see that an item changed in in the play but they're all here in orange so we'll go back into the configuration folder and see what changed so I've still got three files at this point in time this is just overwriting the same file over and over we can do version control this can be put in to get it can get to get hub all these files configurations can be version controlled but this example is just showing you the basics of how this operates so we're going to cap our one text and again you can see now we've placed another line into our configuration so that is the basics that's how it works the main element to this is the folder structure and the files that are in there and if all that is in place this is very simple very logical and this how it works so go ahead create this folder structure all the files for this are on my github so if you click the link in the description to the github you can pull those files and have it ready to go and I have a play with it you know just see what ansible can do very small steps first get the hang of it understand how it works and then this will scale as big as you want thank you for watching I hope you've enjoyed this video if you've got time please subscribe and leave a comment if you've got any questions and I look forward to seeing you in the next video my name is roger Perkin and for more tutorials on networking please visit my website which is www.hyken.com you K thanks for watching
Info
Channel: Roger Perkin
Views: 41,673
Rating: 4.8133335 out of 5
Keywords: ansible jinja2 template example, ansible jinja2 template tutorial, jinja2, ansible, ansible cisco config, ansible network config, network automation with ansible, roger perkin, ansible for network engineers, ansible cisco, ansible network automation, ansible templates, network programmability, network automation, jinja2 tutorial, jinja2 template tutorial, ansible cisco configuration template
Id: p0C90zIbe5s
Channel Id: undefined
Length: 18min 56sec (1136 seconds)
Published: Fri Jun 30 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.