Ansible: Create and deploy a role from scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to another video this time on how to create an ansible role from scratch and deploy uh and run it with a deploy file so i'm going to go over all the steps that we need to cover again everything is in a nice easy gist for you to follow along now this just and all of the contents is kind of complete but the way i'm going to do it is we're going to build it up layer by layer and do step by step so we're going to create the role we're going to set up the tasks and use kind of best practice on how we do that we're going to add templates with some ansible facts we're going to set up some handlers for when things change and they can be restarted we're going to make the whole thing item potent so that the tasks only run when they need to if something changes we're not just going to run it every time and then we're going to show how variable precedence works so we're going to use a default value then we're going to use the local rollva variable and then we're going to break down and use group files all in the main section where i'm running the roll from and you'll see which one takes precedence over another one it's quite a good thing to know if you're doing anything with variables inansible so you understand where it's going to run from so we're going to go through all that if you don't know how to install ansible or you've got other questions check out my channel we've got a whole lot of videos and all my meetup groups and all their chats on there there's loads of loads of stuff on there to keep you busy for ages if you want to know how to do things like jenkins pipelines or how to do cis compliance you can there's some videos on there even how to create an azure instance so just pop along subscribe like like the videos you like there's some great stuff on there okay so let's crack on straight away so the first thing we're going to do i've created a demo directory there's nothing here we're just going to create a roles directory so everything lives inside a roles directory again it's real time so just skip the bits you don't want to see some of it you may know you may want to skip onto the variable presence at the end so i'm going to create directory called roles procedure roles and then from here we're going to use ansible to create the um role for us we're going to call it web server nice and simple probably done a million times that creates it if we look at tree for web server so that this is the default layout everything is there and i do something similar here so as you can see so we've got the roles we do the init and we've done three websites so we can see what we've got so next we're going to set up our um main.yaml so everything's in the right place so i'm going to go back and do everything now from our default directory so here we've got a roles directory so vi into roles and web server and then into tasks and into main i'm going to edit this file so i'm not going to create one massive main.yaml file what i'm going to do is i'm going to introduce a loop which is going to look at an install.yaml and a configure.yaml this is because i feel it's best practice to break out the individual tasks into their own separate components it makes it easier to manage if you need to make updates it's only a small update in one file as opposed to maybe changing a large file um there's loads of advantages but this is how i i like to run this so here we go so i'm just going to cut and paste this in we've already got the first three hyphens so the first thing i do is set paste because i haven't set my vim oh i haven't set my bin variables yet so if i paste inside it all goes a bit strange we make it slightly bigger okay make sure to paste that right so what we're doing here is we're just literally including tasks and the web server tasks refer to this loop control variable here web server tasks and then it's actually looping around install.yaml and i configured.yaml so if we save this and then while it's changing size let's make that a bit bigger right so now what we'll do is create the install and configure files so vi so in the same place i'm going to call it install yaml whatever it was called in the other farm and in here we are going to add in the tasks that we need for this to work will it paste oh wait let's undo that i really could update my vm settings okay so this is just going to use yum to install apache we're going to manage the service httpd and we're going to make sure it started and then we're gonna add a firewall rule i mean it could disable firewall d but it's best practice really to just enable it for httpd so um i'm gonna leave this one hashed out for now we'll come back to this i'll come back to this in a minute when i set up the notify because notifier will go to a handler and the handle will then do a restart so i just want to get a few other things in place first okay so next up there's a few files here um ansible.cfg just like the configuration file for ansible for your local version of ansible running um there's a few things you can add we're gonna add defaults and we're just going to add in inventory equals hosts so i haven't actually created that yet but what this means is when we run ansible locally we don't have to declare where the inventory file is it just looks in ansible.cfg sees that it's already configured and the location only looks for that file i don't have to add it it makes my command line much shorter save that okay let's um be either host or any file so this will exist so again use your own information this is all mine so your users your keys your ip addresses they're going to be wrong but what we're doing is we're creating a web server group inside our inventory we're adding the internal ip address about aws server that we're going to use um we're also giving it some variables so web server column files and then we're using ansible user to that's the user we're going to connect as and then we're going to use this private key here to supply credentials so save that file okay and we also need a deploy file so whenever i run a role i call it deploy file you could call it provision file you can call it anything you want it doesn't matter but the deploy.yml is where i run my tasks from so let me copy this in so um this is true of all kind of playbooks so instead of running a playbook and having all this information at the top and having all your tasks we're sectioning out to roll so the roles have the tasks in and this is a section that tells you where to run and what what to use so we're calling it deploy web server it's going to run against the hosts in web server so it could be more than one that web server is the group name in the host or any file become true because we're going to be installing software my ec2 user has got pseudo privilege to root so we're going to become true so when i run a young command it will just install httpd and then manage the service and then the role itself is called web server so if you were to create other roles you would also just list them here underneath this in exactly the same fashion so i'm going to save save that and right now we need to create a template but let me have a quick look at rolls web server tasks install so we can see here yeah so we're just going to install it here so let's let's actually just run that now on my server so it's yeah so we're just going to do ansible hyphen playbook deployed at yaml we don't need to play an adventure file or any users because all those variables and that everything like that's been taken care of for us so if we just run this it should connect locally and right yeah we didn't actually create this let's create the file so vi roles web server tasks and then configure.yaml just save that it doesn't have to be anything in it let's do a deploy okay because it's empty it just ignores it so we're going to install this version of battery we're going to start the server so we're going to add to the firewall rules so brilliant so let's just go to our web server let's get the ip address just get off screen one second okay got the ip address pop that in there so now we get to the http server test page which is great which means that it's installed our server installed our package correct started the service it's that added the firewall well that hasn't restarted so the ports open and we can now get to it which is excellent okay so what's next so now we need to go into the configure side so if we go back to our file and we look at the config.yml and now we are going to manage the web page with a template i do need to fix this okay so what we're going to do is there's a template directory so we're going to create an index.html.j2 and that that is the destination which once it's put in place will then display instead of the the test page that we can see here so let's save this and then if we vi go into the templates directory and we are index.html.j2 to keep everything the same and then i've given a block of code here so just take that code and i'm just going to hi this one here so where it says message brought to you by i'll add this back in later let's call this first there we go right so we've got the template um we do need to add in a restart httpd so what we'll do let's go to the handler and the main and in here let's add in the handles that we need to add so one is for the restart of httpd and the other one is for the restart of the firewall okay now and we'll also look at the tasks for install and we'll unhash that okay so i think i've done everything let's let's run it there it is let's see if i've got everything so this should now put our page in place with the right template um okay that's done so it's running the restart of httpd didn't do anything to the file also because it's audio excuse me it's already added so now if we go to the test page and refresh here we are so we're now back so obviously i've got a slight mistake here oh yes i'm missing a chevron but i can fix that so this is our first thing so we haven't put in a default variable yet so let me fix that straight away it's in templates so it needs that save that [Applause] i might just run it again just to make sure it disappears okay this is quite a small roll okay so if we update that yeah it's gone so this is the first thing we're using and we see here that we've used ansible host hostname as well so that's there okay so let's go back what's next on the list so we're gonna add a variable so if i go into roles web server defaults main.yaml and i'm going to create a variable so nice and simple variable we're going to go for number one actually let's keep it to the document let's go for two is that right no there's one so have that as one and then if i go back into the template and this time get rid of first and add in variable it's going to pick up variable one in our defaults this is the section on variable precedence so if we just run the playbook there's been a change so it picks it up restarts the fire the service so we can see here that we've got variable one now so now let's update so instead of using defaults let's use let's use files now vars has a high precedence to defaults so if we go into main this one [Applause] let's do variable and we'll call this one two so we know where it's coming from let's run it again so we don't touch anything else we've updated variable we're deploying ansible we're running our role it's picking up this new variable because of variable precedence and that's then being pushed out so yeah it's now changed to two so let's go again so this time we're going to in my directory i'm going to create a group vars directory so make directory group underscore vars and then vi so this here we'll take the name of our host group called web server so that's vi group bars web server vi group bars web server so it has to be the same name if i put web servers and it won't work so webserver.yaml make it a proper file and now that we're going to do variable free okay so now that's updated what do you think is going to happen make a guess so group bars from this section will it will it override the vars for the role so let's see what will happen let's run the deploy so interestingly nothing happened so if we look here we do a refresh it's still two so it's still taking the local roll variable not the default vars but the actual variable for the roll so if we were to take that out so we just hash out files and run again it should now ignore bars because it's obviously hashed out ignore defaults because the group files variable precedence is greater than default but less than vars so we can see something's changed let's do an update it's now free just an import lesson there so if you have anything in group files at this level you're overriding it with vars in the role vars in the role will win so you have to be very careful where you're setting your variables otherwise you'll get the wrong behavior and you'll have to try and work out what's happened and in this small example it's easy but as soon as you start looking at a role of multiple roles doing multiple bits of work it can get quite complicated a bit tricky to find so that brings us to the end of the demo um i hope you enjoyed it the president is quite interesting how ansible decides what what trumps what and it's really important to know exactly how it does that so like i say don't forget check out all my other videos i hope you enjoy going through this one and seeing the role check out the gist with all the information in and i will see you in the next video have a great evening cheers [Music]
Info
Channel: London IAC
Views: 338
Rating: undefined out of 5
Keywords: ansible roles, ansible, ansible roles tutorial, ansible variable precedence, linux, devops, ansible tutorial for beginners, ansible tutorial, ansible playbook, ansible template, ansible templates, jinja2 templates, ansible role tutorial, ansible precedence, ansible handlers, ansible roles explained
Id: 6ie4T3IXcyM
Channel Id: undefined
Length: 17min 14sec (1034 seconds)
Published: Tue Sep 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.