Ansible Automation | Ansible Roles, Templates and Tags

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the session friends my name is Yogesh in this session we are going to discuss about NC boat rolls friends to understand NC ball rolls a little bit understanding of ansible play books is required because rolls are next level offensive play book so you need to have idea in case you don't have idea I will recommend to watch previous videos which I posted sometime back so friends who have NC will play book idea so rolls are basically next level of abstraction of ansible playbook to explain it in better way let's say in your lap setup you go to two servers and you go to very basic configuration you are installing couple of packages you have copying files that's it so playbook is sufficient but in an enterprise set of where you go thousand of servers they are performing different functions or I can say different duties let's say some servers are acting as database over some are application server some are some customizer and moment so friends are to have a single playbook that makes things difficult to manage if you are using ansible what are the benefits of using roles so you can reuse the play books for different purposes if you use roles you can segregate the server functions or server functionalities in terms of roles let's say you got one common role in common role you go to all the infrastructure stuff like to configure your ntp server to install or configure DNS client to install backup software whatever whatsoever so that's example only friend job and you go to one custom role which is for your web server you go to additional custom role which is for your database server configuration so in that way rolls are very useful structure of rolls so when you create roll it code directory structure which code files that contains regular files those need to be copy to target service handler are basically event handling event handling mean let's say you got one task once that task is finished successfully it will call that handler or it will perform some action depending upon your configuration meta directory that is basically role dependencies so assume you go to roles one is database server one is application server so you want database services to be started or configured first then application server so you can set dependencies inside major template so friends template is similar to files only thing template contains files but they contains dynamic data when I say dynamic data it mean you can point to variables so I will explain in technical session tasks tasks are basically simple playbook tasks to install some package to do some copy stuff which we already discussed in PlayBook variable or group variable that Friends variable definitions so let me show or explain these things technically let me open one party session so friends this is my in Siebel control server and this is a client one which is laps over one this is client - which is web server one Here I am inside ansible configuration directory which is HTC ansible that may vary depending upon your setup if you customized it or if you have configured it to some different path right now I'm using default here I have configured this web stack directory I am seeding into this one so friends are that this directory look like and variables which I said let me show you how variable look like first let me show you three of this trajectory so group variable inside that I got one file called all so that contains a or variables then I go to one file host which have detail of my host that's a license information that's a main door chamber which is my main playbook file this is readme and these are roles in roles I go to roles one is common one is web so friends in common role I go to infrastructure configuration which is to configure NTP server and resolve dot corn which is DNS client and this is the playbook to configure these tasks as you said these are files ntp doubt conf and resolve calm and this is the main playbook which copies these files to target machines which are ansible clients and this is the handler and let me know if one of the action is done so perform this trigger to start service for example and let me show you content of this one means all directives and similar is a different role which is web to configure web services so to start where the I will show you a host group variable file first sorry directory ad group variable sorry copy pasting copy pasting not works and here is quote one file all in that I go to variable one is DNS server which I got this value one is variable is NTP server where I got this value okay so that is variable definition friends let's say in your setup you go to three different DNS servers for one separate folder one separate for pre prod one separate for prod I am just giving example I know that's not a very good example but again now and similar ntp you go to different entities for different environments so you can have ntp server prod and TP server dep like this so these are the good periods so these variable values will be cold when we are using these values in templates as I said this is the dynamic data okay so let me show you next one which is my role I am just going to CD roles comon I'm inside common role law inside this role I go to these tasks which is my main playbook file in this task I got first task to configure DNS client what it is doing it is copying the using template module template module basically search what are the group variable values we have defined sources resolved out convent destination is this one it is a result of confirmed target then it is installing NTP it is using your module install NTP state present and it caught tack I will explain what tag mean so it's very useful feature to using tags and it is coping ntp file it again using template module this is a source this is the target and this is a tag and TP and it is notifying once this task successfully finishes if any changes happen basically then it trigger restart ntp service and that's basically handler means we are handling this event let me show you how we handle event I am inside antler and this is a main duty arm up each directory got its on male domain dot Yama file and here if you notice this definition or this statement is matching these two at NTP service which will call this particular handler which is a restart NTP service this will restart or start the NTP service depending upon your configuration so that's a handler friends okay now let me show you template how templates differ from files so let me give you example of this one and TP dot one if you see this particular line I have not hard-coded it to server whatever the NTP server name I have said NTP server in braces so it is checking what is the NTP server value and group variables and in group variables we got this value as I said in case you go to NTP server for prod NTP server plot this value NTP server would have different value you can call that value here here you have to change NTP server pro or anti p server dev so what it means friends are no need to rewrite multiple line of codes you are just calling variables so that is easy and that is easily manageable manageable you know like you are doing this stuff in common role or whether you require that in any particular role instead of doing it on also over so that thing that makes things easy okay so that's common role definition and have explained what our handler is let me show you web role now in web role again I go to handlers and tasks but one thing different in common role I go template here I got file so that's only to explain you what is the difference between hand files and templates so let me show you file contents because files contains static data which is not changing you see in this file I have not hold any variable these are the static values so I have used files that is a whole purpose to explain why we need template why we need files again explaining template for dynamic data which is changing files for static data which is not changing or persistent okay so here in tasks I go to task to install Apache packages and Friends here I'm using your module name is equal to item so that's again new thing for you I believe I have not discussed this thing in my previous video state present item mean with item what are the benefit if you see I have used two packages as TPD httpd tool so it will install both packages otherwise if I use like yum name is equal to httpd state present I have to get separate entry for this package so now with item I can define multiple packages so in single task whole packages will get installed so that's easy that's efficient way for installing multiple packages as next task it is coping index dot HTML file which is static file as I explained some time back and this is a destination tags Apache and it is notifying to restart Apache service and let me show you Handler and this is the restart Apache service handler which is restarting HTTP service not really studying basically starting or you can use restart so I want to get confused so that's up to you whether you want to restart or start ok one thing friends if you are using handler this definition need to be hundred percent same if you say restart here in name and in name you put start or stop so it is not going to work because notifier search the name in the handler configuration so this things need to be matching ok so friends that's a config now let me show you host file I should go out ok let me show you host file now at host ok here all which is a group all where I go to both servers both are my ansible clients in second group which is web servers group I got only webs one I got only one server which I am going to configure as web server so now let me show you my main UML file which is my main playbook here are you have to pay attention because that's the main thing and it's very important to understand in many amel file I included old service means this playbook can control all the server's next tasks first task I got apply common configuration ok here I have mentioned host role this configuration needs to be applied on all servers use and radium user become yes mean become root on other server other mean the target and Sybil client become method pseudo using pseudo and task in task I mentioned coal roles common so it will be calling common role whatever definition of playbook inside common role everything will get cold and curve that will be actioned okay so this common role will be happening or getting action Don all servers okay now I go to second task which is to install and configure Apache hosts web servers so whatever the servers part of web service group right now that's only one server web server one that will get this particular role web role installed and configured so that's a benefit friends so you are calling two roles but on different set of servers so now you can understand like what is the benefit of using roles so you can better manage play books and the ansible configuration okay now let me show you on the server right now there is no etc' result conf sorry it's there but let me delete it RM e TC resolve dot con okay I have deleted this file and let me check whether and tip is there or not and T P Q minus P or yeah you can check package or you can run this query so there is no ntp over i can check RPM is QA crap and TP and tip is node and TP date is there but ntp package itself it note there so that is fine and similar let's check on web server RM e TC result dot confab deleted this file RPM - c httpd and TP if package is there okay both packages are not there so that is fine so friends if you see these servers are not having any configuration so what is going to happen with this playbook the common configuration will be applied to both so which is DNS and NTP in our case the Apache configuration will be applied only on one server which is part of a web service group in our example that swept server one it will be not getting applied on lab server 1 because lapse of 1 is not part of web server group so let's play with it friends ok so here in Siebel playbook your main PlayBook name which is main dot humor you can change the name to whatever you feel comfortable you can say it your web stack configuration web stack dorium work whatever you want to code so that is fine all the extension keep it yml so one can understand that playbook - I I is inventory file I am saying host otherwise it will pick default file which is e TC and Siebel host right now I am using host file inside a web stack directory okay you can change this in NC ball dot CFG we will discuss that in some other play other session so I am running this now so okay if you see first step is completed which is checking both servers are online means clients are online next step it is applying common configuration which is common role if you see this is a roll task for common roll first task configure DNS client it changed it mean it configure DNS client on both servers next task again part of common role install ntp file it copied the ntp file it that's the reason it is showing changed next task pause handler as I said once this copy finish it will call handler so it cold handler for both servers and Angela did it work if it fails it will give you one error message like it is failed and this playbook will terminate but right now it is completed next task is to all and configure web server if you see it only happened on web server one it not happened on labs over 1 because lab server 1 is not part of web server group so it installed Apache it install this package that is fine it worked fine then it copied Apache configuration file which is an index dot HTML from ansible server to target client and that worked well then it restarted the service so friends that is a whole purpose of this particular roles and that worked fine so let me show you if you see let me show you unless - etc' result con you see file is copied it just got copied and let me show you content so it caught this value from variables right and let me show you and TP if you see NTP is worked and NTP service started and on this server Apache shouldn't be there okay if you see that is a patchy tool packet but the patch is not there so that is a configuration which we did and let us verify second server catch et Cie resolve con file is there this quote right entry and TP and tip is working fine that's good so on this one HTTP should be there because ansible was configured to install extra TP d RPM - q httpd okay package is there and let's verify the configuration or not configuration index dot HTML got copied or not okay if you see file just got copied so network contents okay so these are the contents so let's test it friends whether that Apache okay one thing we can verify where the service is started or not because handler was configured to start s to TP d service ok service is running that's good so let's try to open one web page cool this is the IP of server so hello Would here I go to encode hello Would so that code is print mean Apache configuration is working so friends let's away and as I mentioned I have used tags so let me tell you what is the purpose of tags I want to show you again how tags are useful I am doing cat roles let's do one common role common tasks main dot Yama so here for DNS configuration we go tag DNS for ntp we go to ntp and this again track contain NTP configuration stock so when I use tag and TP it will do these two tasks which go to Antibes attack if I use tag DNS it will do only this particular task not only in this playbook what whatever the playbooks inside this directory it will do it on all play books so let me show you how it works let us say on this one I didn't eat NTP con DNS configuration let us try the DNS that's easy RM etc' resolve ok I have deleted this file let me show you again there is no 5 so now I am going to run this playbook and I am using tags DNS so what it is going to do it will not do any other function it will do only the tasks which code tag DNS so in our example only this particular tasks go to DNS tag and it will do it on also was because that's part of common rule or I can limit it but I want to run it one common role if you want to limit it how to limit you can use you can put minus l and server name laps over one and that way you can limit it but right now I want to run it on also us so I have just used tax DNS so what it will do it will copy the file on the server where it is missing if you see on web server one it's green it changed nothing so it means file was already there we deleted file only from lab01 so that's a region it is showing changed so everything worked fine nothing failed failed zero failed zero for both servers that's good and let's list the file again if you see file got copied so that's a purpose and benefit of using tax you can limit your actions if you go tag values so thanks for watching this videos friends if you have an the query or question or even suggestion just post a comment on my youtube channel this video helped you just like Twitter and share it with your the friends I want everyone to learn and if you have any suggestion I personally need suggestions to improve or I can do better for the community thanks a lot transfer by
Info
Channel: Yogesh Mehta
Views: 64,828
Rating: 4.850594 out of 5
Keywords: CLOUD, NTP, CENTOS, EBS, EC2, ANSIBLE, HOW, LINUX, AUTOMATION, AWS, EFS, TO, VMWARE, REDHAT, DEVOPS, DISK
Id: FHSNM5yAUm8
Channel Id: undefined
Length: 23min 45sec (1425 seconds)
Published: Fri Nov 18 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.