Ansible Roles Explained | Understanding Ansible Roles | Ansible Tutorial | DevOps Training | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] ansible allows us to automate the configuration management of systems and add any number of clients as we wish to however have you ever wondered how complex this can get well trust me guys this gets very complex when you have large number of playbooks so how do you think ansible makes it seem like a breeze well that's by using the concept of ansible rules so high all the society from Ed Eureka and I welcome you to the session on ansible rules in the session guys will focus on what our hands will trolls and how you can create and use them so without any further ado let's take a look at the topics for today's session so the topics for today's session are as you can see on my screen we'll start this session with an introduction to waters ansible and then I'll talk about ansible playbooks now ansible playbooks is an important concept to understand because to create a role you should know how to write a playbook after that I'm going to talk about what our answer bill rolls and then I'll show you how to create an ansible rule once you understand how to create an ansible role we'll look into the ansible role directory structure and finally we look into the hands-on part where we'll install a main stack application using ansible roles so I hope I'm clear with the agenda guys so before I start with the session I would like to request all of you to subscribe to her ed Eureka YouTube channel to get daily notified on the top trending technologies also if you're looking for an online training certification program and devops then check out the link in the description box below on that note let's start with the first topic for today's session that is what is ansible ansible is an open source IT configuration management deployment and orchestration tool which aims to provide large productivity gains to a wide variety of automation challenges this tool is very simple to use but yet it is very powerful to automate complex multi-tier IT application environments so guys if you have to understand ansible then ansible is a configuration management deployment in an orchestration tool and uses the push approach so what I mean by push approaches in push approach it does not require any agent setup on individual nodes like the pool approach requires also the push based systems are completely synchronous and you can see the changes made instantaneously and can fix the system if the change is caused problem so the push approach is where guys you do not require any agent set up on individual nodes whereas if you compare the push approach with a pull approach I would say the pull works in a master/slave architecture which requires agent set up on all the slave nodes apart from that the push wave systems are completely synchronous and you can see the changes made instantaneously and can fix the system if the changes caused a problem so ansible uses the push approach guys and is basically used for configuration management deployment and orchestration so I hope I'm clear with this point over here so now that you have an idea about what is ansible let's look into the next topic for today's session that is ansible playbooks so if I have to define ansible playbooks for you then ansible playbooks are the access points to answer provisioning and I written in the Yama format so Yambol is yet another markup language and this is the former that is used to write playbooks apart from that ansible Playworks provide a way to deploy and configure remote servers on various environments and on a higher level you can understand that these pukes I used to handle multi tier rollouts and load balancing tasks for the server's so guys play books and ansible are very simple they're used for configuration files and can be also used in many applications where the data is being stored the written in the Yambol format and it is basically a human readable data serialization language so since I've told you about ansible play books let me consider a sample playbook to make you understand better so the sample playbook that I'm going to consider is to start the Apache HTTP D program as you can see on my screen every ansible zml file starts with three dashes in the starting so those 3 dashes represent that it's a Yama file so for nearly every Yama file your file starts with a list and each item in the list is a list of key value pairs commonly called as a hash or a dictionary apart from that one more important point that you have to consider while writing a Yama file is the Indian station of this file so guys when you're writing Yama file or when you're practicing how to write play books or maybe if you're writing a play book to configure some file in ansible then remember that the indentation of Yama file is very very important so every Yama file as I said before starts with three dashes and this proper indentation for every item in the list so as you can see on my screen this is a sample playbook to start the Apache HTTP program where we have three dashes that represents the start of the Yama file and then we have few items in the list so let's discuss each of these one by one so starting with the three dashes as I mentioned before every ml file starts with three dashes and that is the only significance of three dashes these three dashes tell ansible that it's a Yama file which is rings written after that comes the host so the hosts are a list of one or more groups or host patterns separated by colons so for each play book you get to choose which machines in your infrastructure to target and who is the remote user who's going to complete those tasks so which machine in your fur structure that you will target is the host so to include the horse in the ansible inventory what happens is that people generally use the IP addresses of the host machine or you can give the name so over here as you can see guys my host name is web service after that comes two variables so ansible uses the variables which are defined previously to enable more flexibility in playbooks and roads so they can be used to loop through a set of given values access various information like the host name of the system and the place certain strings in templates with specific values so as you can see on my screen we have two variables over here that is the HTTP port and the max clients right now one important thing that you have to remember over here is that you know ansible already defines a read set of variables individual for each and every system so whenever ansible runs on a system all the facts in the information about the system are gathered and are set as variables but there's a rule for naming the variables variable name should be letters numbers and underscores and variables should always start with a letter so please remember these rules when you're defining variables in ansible after the variables comes to user so user as I previously mentioned will be the knee of the user who is responsible for completing the task so over here I've mentioned the user to be as a root now after this is done the next section that comes into the picture is the tasks section so the tasks section as the name suggests allows you to break up bits of configuration policies into smaller files apart from this the task includes the pull from the other files so as you can see on my screen I've mentioned the task name to be as ensure Apaches of the latest version and I mentioned the command that can be used to complete the task similarly I have another task that is write the Apache config file and then I've mentioned the template name after that I've also used to notify directive in this particular task and then I've mentioned the third task status ensure Apache is running so for that I've used the service so that's how guys you can declare tasks now after that comes the handles so handlers are again just like regular tasks and ansible playbook but only run if the task contains who notified directive and also indicates that it has changed something so for example if a config file is changed in the task surfacing that config file may notify a service restart Handler so that's the reason you can use handlers and then over here as you can see on my screen I've mentioned the name to be as restart Apache and then have used a service restart handler so that was about this particular playbook guys I hope that you've understood how to create a playbook using the Yama format you start with three dashes and then you have a list of items and all of those little lists of items will be put into a proper indentation format so I hope I'm clear with this point so if you want to understand more about ansible playbook you can refer to our session on ansible playbooks apart from that since this session is going to focus on ansible roles let's move on to what exactly are ansible roles so now moving forward with our next topic that is ansible roles and simple rules is a concept that deals with the ideas rather than events so it's basically another level of abstraction used to organize the play books and they provide a skeleton for an independent reusable collection of variable stars templates files and modules which can be automatically loaded into the play books so what's basically happening is that you know the play books are a collection of rows and every role has a specific functionality now I'm sure by listening to this technical dogs you might not have got a good understanding about what are answerable roles so let me explain you with an example over here suppose you want your PlayBook to perform ten different tasks on five different systems would you use a single playbook for this well I would say no using a single playbook will make it more confusing and obviously it would be more prone to blunders so instead what you can do is you can create 10 different roles where each row will perform one task then all you need to do is mention the name of the role inside the play book to call them so isn't that simple guys instead of using a single play book to perform 10 different tasks on five different systems what you'll do is you'll just create 10 different roles and those 10 different role names can be just mentioned inside the play book to just call them so that's how you can use ansible roles guys apart from this there's also one advantage of ansible roles that as ansible roles are independent of each other so the execution of one role doesn't depend on the others and hence they can be reused now if you look at a large-scale industry guys this advantage gives many benefits to the users using ansible so with ansible roles you can modify and personalize these roles according to your requirements and obviously this reduces our task to rewrite an entire section of the code every time we need it thus simplifying our work so if you just consider the example that I've taken before we had written 10 rows for 10 different tasks now let's say you have to use five of them for another set of provisioning now do you think you have to write the entire playbook again well I would say no what you just have to do is you have to just reuse these fire rules by calling them in this new playbook and you can make the modifications required as per your requirement it's very simple right so let's say for example you have to write a playbook for setting up lamp stack now for that you have to create four roles each for creating Linux Apache MongoDB and PHP now in future let's say you want another playbook for setting up lamp stack as well as WordPress now instead of creating new roles for lamp stack and WordPress what you'll do is you'll just simply use two older roles that is the roles used for LAN stack and additionally you will just create a new role for WordPress so that's how guys you can use ansible roles so now that I've told you ansible goals let's understand how to create an ansible rule so to create an answer we'll roll a tool called ansible galaxy comes bundled with ansible so over there you just have to use the syntax that you can see on my screen that is ansible - galaxy in it and then you have to mention the roll name for example let's say you want to create a sample roll name so for that you just type an ant simple - galaxy in its sample roll name let me just show you how it happens now to create a roll what you initially have to do is you have to go to the ansible directory so for that what you'll do is you'll type in CD / e TC slash ansible you'll go to the statically first and let's just list so when you list you'll see a directory of roles so we'll go to roles now once I go to the rules part I'll just list so you see that you know we do not have any rolls present so to create a roll what you'll do is you'll type in the command sudo and sibyl's - galaxy in it and then you'll mention the roll name so since we wanted to mention the roll image sample rolling I'll just mention samples roll name and then I'll click on enter once I click on enter I'll just mention the password and then I'll click on enter again and once we click on enter again you'll see an output that you know the roll has been created successfully so let's just wait for that to come so as you can see on my screen we can see that you know the sample whole name was created successfully so that's how guys you can create an answerable roll so now that you know how to create an ansible roll let's move forward and understand the directory structure of an ansible roll so the directory structure of financeable roll is as you can see on my screen we have defaults files handlers meta read MD tossed em please test and VARs so talking about the default directory first well the default alpha contains all those default variables which are going to be used by this particular roll after that comes the files the files contains those files which can be deployed by this role so it contains the files that need to be sent to the host while configuring the road after that comes the handles so the handles contains the handles which may be used by this role or even outside this particular road after that comes the meta so meta as all of us know defines the meta data for this role so basically it contains all the files that establish the role dependencies moving forward we have tasks so tasks contains the main list of tasks agar to be executed by this particular role and it contains a Hamel file for that particular role itself now after that we have templates so templates contains those files which can be modified and added to the hosts pain provision so ginger Doe is the template language which is used to achieve these modifications after that comes the test so the test directory is basically used to include the testing part and ansible playbooks so when you define the role you have a test directory and this particular test directory is used to achieve the test for that particular role after that we have the VARs so this time trick contains the other variables that are going to be used by the role so these variables can be defined in your playbook but it's a good habit that you define them in this particular section so now that I've given you an overview of a directory structure let me open the directory structure of the ansible row that are created I'll just go to sample role name and I'll list so once I list you'll see all the directories that I've mentioned before so that was about the directory structure guys now let's move on to our hands-on part where we'll install a mean stack application using ansible role so to install and means that application using ansible role will be just executing a single playbook and then we will have three roles one to install the prerequisites want to install MongoDB and one to install node.js so I hope I am clear we just have one playbook and we'll have three different roles that is to install prerequisites MongoDB and node.js so without any further due let's move on with the hands-on part so to start with the hands-on part what we have to do is we have to initially create three rows that is the prerequisites the MongoDB and no cheers so what I'll do is I'll go back to my screen I'll just come out of this particular section and then I'll use the same command to create a row and then I'll mention prerequisites and then I click on enter similarly I'll create for MongoDB and then I'll create four noches so as you can see on my screen we have created three roles that is the prerequisites the MongoDB and no chairs role now once your role has been created our next step is to configure our roles so to configure our roles what you'll do is you'll go to your first role that is the prerequisites role so you'll go to prerequisites and then you'll go to the tas section of this particular road and then you'll write a yam l file for this particular road so to write the Yamaha you'll type in sudo VI main dot let's say yeah Mel will mention yml and then click on enter now once you click on enter you'll see that you know there's a file open where you have to mention the tasks for this particular role alright so as you can see I am L file is open so our initial prerequisite is to install git so for that we have to mention a task in the prerequisites role to install the kid so for that what I'll do is I will just copy paste from this particular section and then I'll show you how you can write it so as you can see I've started my ml file with three dashes and then have mentioned the task name to be as install gate after that I'm using the app modules so that we install gate using the app module and there have mentioned the name to be asked it so the state will be present which means that you know ansible will try to install it only if it doesn't exist and after that I have also mentioned the update cachet to be yes so that is because you know we want to update the repository list before installing this package so for that reason I've mentioned update cache it to be yes after that what I'll do is I'll just save this file and exit now once I save this file and exit what you have to do is you have to go to a different file that is you have to configure your next role that is your MongoDB role so you'll come out of this particular role then you'll go to the MongoDB role again you'll what you'll do is you'll go to toss and then you'll create a file sudo we I let's say main dot yeah Mel and now once the file opens what you have to do is to install MongoDB that is a task that you have to mention in MongoDB role is that you have to mention tasks to import the MongoDB public key then you have to mention the tasks to add the MongoDB repository after that you have to mention the tasks to install MongoDB and finally check if the service is running or not so for all of them will define the tasks in the MongoDB role right so as you can see our main Dom file has opened up so what I do is I'll just copy paste from this particular section again and then I'll show you step by step what I've mentioned right so let me just copy paste so as you can see on my screen we have mentioned four tasks the first task is to import the public key the second task is to add the repository the third task is to install MongoDB and the fourth task is to check whether the service is running or not so for that I've mentioned start Mambo D so talking about the first task first so we here if you see I've mentioned the task name to be a small DB in poll published and then I'm using the app key ansible module to manage the addition and removal of public key repositories after that I've mentioned the key server from where the public key has to be taken and I've mentioned the ID of the public key now this gives us a very easy way to define bit server we want to access and also which public key we want to import by giving the ID well that was about the first task guys now moving on to the second task that is to add the repository so to add a new repository I've used the apt repository package after that I've mentioned the file name and the repository name so this is basically the location where we are going to dump the repository context after that I've mentioned the state to be as present because we want to add the repository and we have also said the update cache it to be yes to force a repository update after the addition now once that is done our third task is to install MongoDB so to install MongoDB I've mentioned the task name to be as MongoDB install MongoDB and then have used the app module after that I've used the name to be as MongoDB ox so this is from where we want to install MongoDB the state to be as present and update cached parameter to be as yes once that is done we want to check if either it's working or not by starting it so for that I've mentioned the task name to be a start D and I'm using the shell module to give the command so after this is done what you have to do is you just have to save your file now once that is done you have to configure your third role that is the node.js rule but before that let me just clear the screen now what I'll do is I'll just come out of this DB rule and then I'll go to the node chase role similarly I'll go to tasks section again that is the tasks directory in this particular rule right and then again I'll create a Tamil file so for that I'll mention pseudo we I if main dot shaman right and then what I'll do is I'll just copy/paste from this particular section and I'll show you what all tasks that we are performing in this particular section so as you can see on my screen we've mentioned the different tasks in the noches role for the noches role we have various tasks like to get the script and then to set the execution permission to the script after that we have to execute the installation script and then finally remove the installation script once that is done we'll install node.js and then we'll install NPM and finally we'll install the power and the gulp globally so starting with the first task that is to get the script so to get the script I've used the get URL module I've mentioned the URL from where you have to get the URL and then I've also mentioned the destination where this URL has to be placed basically where the data has to be placed and after that is done our next task is to set the execution permission to this particular script so for that I'm using the file module where we'll mention the part from where the script has to be taken and the mode to be su+ set so that means that you know we're setting the permissions to execute the script after that we have to execute the installation script so to execute the installation script I've used the shell module where I've mentioned the command from where the script has to be executed once the script has been executed the next thing is that you know you have to remove that particular script you have to remove the installation script because you no more need it so for that I'm again using the file module and in that I've mentioned the part from where you can find the script and the state to be as absent now after that is done what you have to do is you have to install noches to install node.js I've created another task that is to install node.js and I'm using the app module where I've mentioned the name to be as item the state to be present and update cache it to be as yes and then I'm also using the wet items parameters the west items parameter works as a for loop so tasks will be executed for each element which is inside the with items parameters so this particular task that is to install node.js will be executed for the build essential and the node.js parameters and once the node.js is installed what we want to do is we want to install NPM so to install NPM what I've done is I've mentioned the task name to be as install NPM and then what I've done is I've used the app module to install NPM and then I mentioned the state to be as present once that is done we have to install the power and the gulp globally so as these packages are need to be installed globally so for that will again use the with items parameter to run as a for loop and then will mention both the Bower and the gun apart from that we've mentioned global to be as yes because we want to install it globally and the state to be as present once that is done what you'll do is you'll just save this file and exit now once this step is done that means that you know all your roles are configured so what you can do is you can just come out of all these roles that is you can just come out of the node chest role and then what you have to do is you have to create a playbook so that you execute all these roles so for that what you'll do is you'll just come out of this roles directory again and then let's say you know we create a playbook over here in the EDC slash ansible directory let's say we mentioned sudo VI let's say mean stack dot yeah mel and then i click on enter now once i click on enter REM will file will open so what i'll do is i'll just copy from this section and then you'll paste it over here so once you paste over here what you see is that you know created a playbook in a llaman format because it starts with three dashes and then I've mentioned the host to be us all become to be true and then I've mentioned watch so ads are all the variables required during the installation so that will be taken from the Waze directory and so will become past ad raker because that is the password for ad Rekha the wire node to be temp and then I've mentioned the roles name that is the prerequisites MongoDB and no chairs so those are the three rows that have created which have various tasks beneath them after add what I'll do is I'll just save this file and I'll click on enter now once I click on enter before you execute this playbook what you have to do is you have to go to your slave machine and let me just open my slave machine in that you have to open your terminal and over here what you have to do is it may happen that you know your key that you're using that is to import the MongoDB has been expired or maybe has been updated lately right so you have to give the update action on the slave machine so you just mention this command sudo apt key list clip expired right and then you'll just execute right after that you'll just mention the ED Eureka password now this is to basically check that whether your key that you're importing for MongoDB has been expired or not so it says that you know it has been expired and 2019 to level so for that what you have to do is you have to update it so you'll mention sudo apt - key adv space double - Keys / HK p key server dot ubuntu.com : 80 space - re cv - 0x these 6/8 fa-50 FP a three one two nine two seven right and then you'll click on enter now once you click on enter you can see that units getting executed so let's just wait for it to execute and then what you have to do is you have to remove the locks we have seen that you know it has been executed and the total number of processed are one now what you have to do is you have to mention sudo su now once you mention sudo su what you have to do is you have to go to the static - your CD slash var slash lip /p PKG right and click on enter now once you click on enter what you have to do is you have to remove the locks from this particular section right so you'll just list the locks right now once you list the locks you see two logs at is lock and lock Trenton so for that what I do is I just mentioned remove and then I'll mention lock - front end right and I'll click on enter similarly I'll mention remove lock right and then I list again now once I list again you see that you know the locks have been removed now what you have to do is you have to go back to your master machine and then you have to run this command of sudo ansible - playbook and then you'll mention slash ATC slash ansible and then you'll mention mean stack thought yam and you'll mention - cake after that you'll click on enter now we are just mention the password and click on enter again you'll see that all the tasks are getting gathered so you'll just continue with yes now once you mention the password and then you see that all the facts are getting gathered the first task that you see executing is from the prerequisites role to install git now over here you can clearly see that we see a status of change similarly happens for MongoDB stars where we see that you know all the tasks are getting executed one by one and finally once all the tasks are getting executed you'll see that it is a state of all the tasks has been changed so that's how guys basically you can install a mean stack application using ansible rules so in this particular hands-on we have installed the mean stack application using tray rolls that is the prerequisites MongoDB a no gist and then we used a single playbook with that we come to an end to this particular session today I hope you found it informative so if you have any further queries related to the session please comment down in the comment section below under then that's all from my site today I hope you found it informative thank you and have a great day I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to any Rekha channel to learn more happy learning
Info
Channel: edureka!
Views: 85,415
Rating: undefined out of 5
Keywords: yt:cc=on, ansible, ansible roles, ansible roles explained, ansible role file structure, ansible roles templates, ansible roles tags, what are ansible roles, understanding ansible roles, ansible roles introduction, DevOps, DevOps tutorial, ansible tutorial, Ansible playbooks, what are ansible, create ansible roles, ansible role directory structure, ansible roles examples, deploy mean stack application using Ansible roles, devops edureka, ansible edureka, edureka
Id: Or6k2UcKeN4
Channel Id: undefined
Length: 29min 13sec (1753 seconds)
Published: Thu Jan 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.