Ansible Jinja2 Template Tutorial Server Automation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to another demo this time on ansible templates and ginger 2 we're going to show you how to set up ansible managed variable which lets people know that your file your managing is managed by ansible we're going to do some simple examples of using ansible facts inside a template we're going to use playbook variables in some templates show how that's used we're going to put an if else conditional in for different outputs and we're going to use uh we're going to create like an etc host file using the ginger 2 template from all the hosts in the group names so you'll see that that's good right so let's crack on so always remember documentation you know here's the link i'll put it all in the um the comment section and it's always worth looking up all the answer documentation it's very good okay so first things first let's create oh yeah so i already have an inventory.ini let's have a quick look so it's just in here for me so i'm looking at localhost a centos ubuntu server i set up some individual variables and then you know i've got my key in there as well your very your inventory will be set up different to mine that's just to give you an example first thing we're going to do is create the ansible.cfg file and all we're going to put in here is we're going to do defaults and then we're going to have the ansible manage section and then we're going to say what it is so basically donated this file it's it's managed by ansible and then next is the template.yaml so this is the brains this is what we're going to be running in a minute as our playbook so it's quite a simple setup i'll just write this out now we're going to run it on all our hosts you can individually run it on different groups within your inventory but i'm going to write on everything just because it'll give us more output using the template module and i'm about to create a file called example j2 doesn't have to be that but it it's a good to get into the naming convention so you can easily identify what files do and we're going to call it well we're going to put it in temp result dot text and we'll keep coming back to check that file so that is it initially so we've done the answer managed we've written a template that will run it and now we're just going to create our example dot j2 with the simple answer managed variable and then we're going to run and support okay here we go it's the same as always ants will have a playbook then your inventory and then the playbook that you want to run don't worry about those dependencies there that's just something else i was working on but it doesn't affect the demo okay so if we now cut the temp result.txt file if i can spell it right there we go we can see that we now have a file that is managed by ansible so if anyone wants to edit that file they go oh hang on a minute let me just double check um i better not change anything because that's paul might come along and put it straight back so the next one we're gonna do is look at various ansible facts that we collect about servers that we run on them you know when you run gather facts and we're going to use that super host host name we're going to use the ip address and we're also going to use the ansport os family so these are available on everything there's a big list of them let's write that one out and then the operating system and this is ants ball underscore os underscore family okay put in a couple of spaces let's save this run this now so now we'll see some of those variables appear in the temp uh the results.txt file so this could be any um file on any server it could be configuration file it doesn't matter we will use these variables so if i ssh onto one of my servers ubuntu one and cut the exact file we're actually updating the same file on those servers too so you can see there's different information in there see that's debian because it's ubuntu server so yeah it works across oss so now let's get on with the next one so this time i think we're going to use a playbook variable so what we'll do is we'll put the variable in here so it's going to write our playbook variable and then in the brackets is going to be var one and then i'm just going to incorporate file one into the template so we just add at the top under hosts so we do vars and then enter down and then we're just going to simply add in the variable one and then what that value equals so bar one and then just hello world could be anything so save that run it oh let's just double check yeah so that looks good let's run that there it is okay templates updated so let's have another look just tap up a few there we go so now we've got the variable hello world so this is all done via the template and the um ginger 2 file okay so let's move on so this time let's do a conditional so first of all we'll collect the ansible mem total underscore mb and then i think we'll just double check we can get that and once we get that we'll then use like an if conditional to tell us to set up basically if memories of a certain size and print out a message and if it isn't print out a different message so it's quite a good way of just understanding how you can get quite clever there we go so total memory is 967 meg very low memory machines so now we're going to do some if conditionals so i'm just going to write this out you can always use equals equals as well if you want to equal something i've gone for greater than 100 just to test it out so we'll get it working at this great 100 because we know it's great 100 then we'll change it and we'll see it flip to from large to small save that again and run that once i've got it right it should work okay yes let's check the file there is some memories large and we know that because we only said is it larger than 100 if it is it's large so now let's change this add a zero so now it's a thousand we know that it won't be bigger so it should flip down and say memory is small let's write again yep changed memory is small so there we go so now we can use conditionals you know very basic version but it will actually give you quite a lot of extra scope in your templates to control what happens in certain conditions so now we're going to create an etc host file type entry so we've got some more ginger tube stuff this time we're going to look at the host groups and this time we're going to select all so this is everything in the inventory you can specify maybe just db servers or ubuntu servers depending on how you've grouped up your inventory we're going to do everything because we just get a bit more so this line's a bit of a long line because we're we're going and getting the individual ipv4 ip address and then we're getting the host name and it's all on one line it looks a bit long but it's just um it's just way it has to be unfortunately so we're going in getting the default ip address and then we open that back up again we start another one and this time it's going to be for the host name now they're not going to be very interesting host names because i'm in aws it'll just be ip hyphen something something but if it was one of your servers on a different you know different cloud or your own servers it would probably have a much more relevant name okay so that looks good just close that up okay i was just running that ah now that's hit an issue um let me just double check ah yes i'll see what i've done i missed the four off the beginning so let's do that here we run and let's check the file and there it is so it's actually gone through the host list and it's pulled out all the ip addresses and the host names so we've created a file we created to see hosts you know straight from the servers are in the inventory so that's pretty clever and if we check the remote servers we can see that we've got the same file on there with the same output so it works across servers so this is pretty good stuff you know just these few simple template sort of how to use template and ginger 2 will give you a lot of options in how you use and manipulate your files so this is good so um let me just check one more server not that this one and change the centos this is just just to check the other server so etc easy to user and then again it's the same so we've added ansible managed to that to our file we've set up some simple facts where the playbook variable we use that in the template we create a nail if else condition we create that you see host like file you know look at the documentation subscribe to the channel and i'll see you in the next video take it easy [Music]
Info
Channel: Dennis McCarthy // Automation Engineer
Views: 9,576
Rating: undefined out of 5
Keywords: ansible, linux, jinja2, jinja2 template, ansible jinja2 template, ansible jinja2, devops, ansible tutorial for beginners, ansible tutorial, ansible playbook, ansible template, ansible templates, jinja2 templates, ansible jinja2 template example, ansible jinja2 template tutorial, jinja2 tutorial, jinja2 template tutorial
Id: eRkAfmlH5nI
Channel Id: undefined
Length: 11min 47sec (707 seconds)
Published: Sat May 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.