Ansible Tutorial | How To Create Ansible Roles From Playbook | Ansible Playbook vs Role vs Task

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] do [Music] hello friends welcome back to another ansible tutorial and in this tutorial we are going to see about ansible roles and we are going to go through step by step process with an example okay so what we will be looking is like we'll be looking into one of our old and spoke playbook which we have written to install lamp stack okay and we will see how we can convert that ansible playbook into different roles so we'll be splitting that playbook into two roles so roll one and roll two so we'll talk about how to create the roles using ansible galaxy and we'll also see what content we will put into those roles so in one of the role we'll be having one task and also a handler again another one we can we'll have only a task so there are other things also we can have it in the role which we discussed in the previous tutorial like what all the content is a role will have it but here we'll be looking more of a role task and playbook okay so that's the overall topic so we'll be doing this in a hands-on you by looking into one of our playbook and we'll be converting that into role okay so before i get into this uh actual uh task how we do that uh i have a request like for those who are new to my channel or those who has not subscribed to my channel kindly click on the subscribe button uh click on the bell icon and also like this videos share and comment okay so in order to start i have a two machine one is the ansible uh one which will be having you know ansible install another one has a local host okay where i'll be installing the contents for using the ansible roles okay so uh before i do the creation of ansible role or anything let me just go to my uh the playbook which we have used okay so this is one of the playbook which i have shown you how to write the you know lamp stack where we'll be installing uh apache mysql uh database php right so we are installing all those things uh and also you know we are starting these services and we are copying some content you know uh to the uh html or www.html folder okay it's a php file and we are going to restart the service only these files are changed okay and we are doing that using handlers okay so what we are going to do is uh we are going to split uh these into three parts okay uh three parts means uh two rolls okay one roll specifically for uh installation part so we'll be having one roll as install where we'll be using these contents for it and also the second part will be copying content okay so that role will be copied uh it can give any name i'm going to use the name as copy okay and inside that copy role we'll have a two part one will be the four task for uh copying what that one and another one for handlers okay so that's what we are going to do it so let me start uh creating the roles first okay so i'll go to the ansible machine okay so i don't have anything over here so how to create a role is i'm going to use ansible galaxy command okay and i would use init and the name of the role i'm using install as one of the rule okay so you can see i know there is a folder created as install okay and uh you also got the message that the role install was created i'm going to create one more role which is called copy okay so if you see uh that role is also uh created now if i go to the role install okay and if i see these are the folders it has created like defaults files handlers meta pass templates test wars right so we spoke about detail about all this so if you have not uh check my tutorial on you know what these roles and what are these folders please check the previous tutorial and or or the link which i have given it in this video description okay now if i go to task folder okay and uh you should be able to see a file called main.tml okay and no i don't have anything inside this because no it's a template we need to put the contents here okay so before i do things on the machine what i will do is i will use my sublime text editor okay i have a i will create a file okay or you know i have already created a similar template so uh if you see i have created a similar kind of uh format in my local machine and i will open this main.yaml which is for install task okay so let me use this content okay so what i'm going to do is i'm going to use this task so uh in order to put the content we don't need the the task over here we just need to have the content from only the name okay so i'll copy this to here so what you'll have it only just uh free high hyphens okay so after that you need to have this content okay so the name will start from here and let me put this one uh to a little bit alignment okay what you need to have is you need to move these things to the left okay so it should be something like this okay so we need to move this uh towards left to make it uh to the alignment otherwise it will not work because it's a yaml format since we copy it it does not work like that okay okay so this is done now let me fix this one as well okay i'm almost done okay okay so uh this should be the file uh for main.dml file for install okay so this will be the content for the task in the install okay so where we'll be using this for uh yum module to install these things then starting the service okay so we are done with the task for uh install now let me open the next file so that should be for copy okay and let me go to the task there okay so all the files you know it's a blank you know that right so because it's a just template so in the copy side what i'm going to use i'm going to copy this one along because i don't want to add the handlers over here because we have a separate folder for it so let me move these things over here okay again to do the alignment okay so let's move the notify as well okay okay so this should be the main dot yaml under copy task okay so you understood like what we are trying to do is we are copy you know splitting the playbook okay so what we are going to do is all the copy part we are going to put it into the task section of the copy role okay and uh inside the installation we have used the install part of the from this uh installed task we used it to the um task in the install role okay now we have a handlers right okay so i don't have to specifically mention handler because we have a folder specifically for handler as well so if i go to copy and you go to handlers you have the main.yaml so again it's a blank one so what we need to do is we need to put the content for handlers over here okay okay so it's not for copy okay it's for handlers okay so yeah sorry like we don't need the handler specifically to mention because this is not a normal playbook so we don't need to mention handlers there okay so we just need to put these things back into the alignment okay so only thing you need to be very careful is the name of the handler what we are using here it should be the same as what we give it in the notify section okay so if it's not the same you know this notify will not be able to call this uh things okay so uh what we have done is we changed all these things into the splitter into tasks and handlers everything so what next thing is like we don't need the these things over here so what it will happen is we need to change the task into roles okay because it's the role how the next part will be we need to mention uh what the role is so the first role is install and the second rule is copy okay so that's how it will be so we just need to change the sections for task into rows so once you change the roles means what it will happen is this role will look for the role so it will go to install it will look for the task it will look for the handlers it will look for the wires and everything inside those folders such structure so in our install we have only task so it will run the task okay main.yml then we'll go for copy so it will run the tasks for copy as well so that it will look for a notify which is for handler so it will go for handler and it will run that okay so let's uh do this now so let me copy this content from main.yaml let me go back to the machine okay so we have the main so i'm going to edit the main.yaml so you can see it's a blank one okay and i'm going to delete this content and i'm going to paste this content okay so i don't need the two three iphone so i will remove one okay so let me save this okay let me go back to the copy role okay because that's all in the install role so let me go to copy okay and let me go to task and i will open main.yaml okay so let me delete this content and i will copy the content from the sublime text editor okay so this will be the content for main.yaml so let me put that here okay so let me save this as well now we need to go back to one step back and i need to go to handlers okay and i have this main.dml so let me edit that as well okay let me go to the sublime text editor again and let me copy the content for handlers there okay so we are done with those things now we need to create this one more file which should be outside of the folder so i'm going to create a new file called lamp stack okay dot yaml and this will contain this will contain the actual uh code okay so this is the only content we need to put there because we just need to mention the host or inventory where we are running the this yaml file or this ncpo file and with which uh user account is running okay so i'm going to put this contender here and i'm going to save this okay just make sure that the names of the roles you are giving it correctly since i have used you know install and copy i'm using that okay so now we are done with all the thing okay so we're setting up the role so now we have done the role now the next part is uh similar like you need to use ansible playbook okay and uh you need to run the lampstart camel okay so once you run it this will call the roles which we have set it in this playbook file okay so let me run it let's see what happens so you can see it has done the first part to check the inventory so it has now going to the install apache because this is not directly coming from the ansible playbook which lampstand.yml right inside that it's actually going to the roles where we have mentioned the role for install that is where this content is coming from so you can see the whole uh process completed so it has run the installation part from the installing uh task from the role so it has installed it has started the service so these three things coming from the role install then the next role was for copying right so it has copied the file and it also run the restart uh service because there is a change in the index.php file right so handler will be running only when there is a change if i run this thing again let's see what it happens okay because you can see all these shows has changed now you will see it's not going to change because you see it says all okay right and here if you see after copying there is no handler because here if you see after copy the handler is running right here if you see the task is okay and there is no handler because there is no change in the file and there is no need for to call the handler okay so that's the difference okay so i just wanted to show you like in what cases the handler will be called just have a look on my tutorial on what is handler so you'll be able to get more information on that okay so i hope you know you understood how we splitted the lambsta camel into uh you know different roles right so we had a one role for install another one for copy right and we have also had handlers and we have a different folder structure for uh you know each rows right and in which file we have to put so the main task will be on the task folder and handler will be on the handler and the other like bars and other folders which we can use it which will see it in the upcoming tutorial so that is all uh with this uh tutorial so i hope this is informative session for you okay and i want to thank you for watching and again my my request would be for uh you is like kindly subscribe to my channel like this video share as much as possible and provide your comments on what you would like to see more okay
Info
Channel: Thetips4you
Views: 17,354
Rating: undefined out of 5
Keywords: Ansible Tutorial, ansible playbook vs role vs task, ansible role vs playbook, ansible roles vs playbooks, ansible roles tutorial, ansible roles and playbooks, How to convert playbook to roles, Ansible roles explained with example, ansible playbook roles tutorial, ansible playbook roles handlers, ansible, playbook, roles, tutorial, thetips4you
Id: jZep1jU396g
Channel Id: undefined
Length: 16min 7sec (967 seconds)
Published: Fri Apr 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.