Ansible Roles - Understanding Ansible Roles

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a very good morning my name is Alex Roscoe and I welcome you once again to network nerds YouTube channel just after Diwali so I hope you had a wonderful Diwali so let's start with a bank and learn about something about ansible so I'm going to explain you and civil role in this video so the prerequisite for moving further in this video is that you should have a knowledge of ansible and civil play books so if you don't have the knowledge of ansible and writing the ansible play books I would strongly recommend you to go back to the playlist and first go through the basic ansible videos on our channel so let's understand what is the ansible role and how ansible role can be used to configure machines in a very effective manner so let me open my G edit my editor here so that I can explain you the points right so ansible roles I hope is visible or I can change it the font also let me make it bold so let me make it bold here perfect I like this and civil rules so number one thing what is the rule so if you are comfortable a bit puppet in puppet we call it modules and if you are comfortable in chefs the chef calls it cookbooks and so ansible calls the same thing a role Wow more technology more now am I supposed to learn puppet and chef also it's solely up to you so a role allows you to configure a group of nodes at the same time and Rover also make your code and symbol code much more modular so the code then can be shared or then can be reused on any number of nodes so a common practice is that we normally have a role for each application or component of your webs of your infrastructure like suppose I am running Apache so I will be having a role for Apache so when I say a role for Apache so that means whatever is required for Apache to run should be within this role right so I can write here whatever is required for Apache to run will be in the role so it's a self-sustained container in itself so as I have told you in puppet decoite modules and chef calls it cookbooks so in this example I am going to create a role for Apache and we'll be installing configuring a web server using a role Apache so before so there are a lot of things that I might need to explain you but so let's start step by step right so let's first create a room so if I go to my controller here this is my ansible controller and civil is installed here so by default the main the working directory for the ansible is SC ansible you might find a certain number of files here which I might have used in my previous videos so I hope you can see this Rose folder so roles should be created in dice inside this roles so if I move inside the roles folder there is nothing here because I hadn't created any road so role means are predefined a predefined structure which should be used to configure any application so if and the best way of learning is by doing it so if I intend to create a role for Apache here so I should be using the ansible galaxy command to create a template for me so I say ansible galaxy initialize Apache that means I intend to create a role for Apache and because I am NOT going to download anything from the internet it's better to use hyphen F and offline here if you see there it gives me a message Apache was created successfully so earlier I have got nothing here now if I give an allele here I will have a role a folder Apache so what is inside this if I use the three command I don't have a tree you so let me just install the tree first young installed tree it will be easier for you to understand the things then install the tree here so if I give the command tree Apache you will see this structure is being created by the ansible galaxy command so before I proceed further you should understand the importance of all these folders and what goes inside each folder now one thing the role should be inside the roles folder no more working and every application server or every every component should have a separate rule so I have a role for Apache in the same manner I can have a role for say FTP or SSH or mail server or anything okay so role is the main container and inside this I have the roles so let us understand these folders here so if I move into Apache I have got these folders files defaults handles so let's understand one by one and then I will write the code fine it's better to open my editor here so that I can write also it will be easier for you and Anne and Anne and wait wait wait okay I can do this that that's much more better yeah so the first folder that you will see here is the defaults right what goes under default so d4 okay I'll just write here components of a road so in my case it's Apache we've got certain folders here number one I had a folder here default I am going as you are seeing it on the screen so default folder will provide you the data about the role or the application so it stores the data about the rule or application in my case it's Apache let's suppose I some some variables might be used some default variables will be stored here before variables which we can use when we will be using the templates for default variables are used here so it's better to have the D in caps him so default variables are used here so let's say I want to run web server on port 80 or might be I want to run it on 8080 any name so the variables will be stored in this folder so this stores my variables okay fine let's move further then what's the next folder next is have what files so files if you are comfortable with puppet and chef the static files are stored here the files which I might be using to push to the remote machines so I put the static files here right the files which need to be added to the machine which is being controlled without any modifications so these are static files files will then be copied on remote machines okay then I have got handlers handles so handlers they are kind of a refresh or trigger so some directives like if you are comfortable with puppet then you must have knowing that notify and subscribe commands so some event which need to be triggered so I can say tasks which are based on some actions on some actions like triggers I'll give you a very simple example in case the con file changes of my Apache the service should restart so that code should be written in the handlers so I can write here in case example can be in case my extra DVD comm changes it should trigger service restart right so that code will be written in the handlers meta then comes the meta folder Mehta as the name says if it tells you about a role what exactly the role is who is the author of the role who has the one on which played forms it's supported so these kinds of things are being mentioned in the meta so I can say information about the role things like author name right the supported platforms etc are stored in the meta and dependencies if any dependencies dependencies if any will be stored here in the manor then go to tasks the main folder tasks so you your actions your actual actions will be stored inside the tasks folder so the core code that I can write here core logic or code will restore it so like installing a package code like installing package copying files etc the actual code which you are going to run will be stored in the task 6 you can see here is the templates as the name says template folder will be storing the dynamic files the template files so these are not static files these are mind dynamic files which I can change as per the node so I can write here it's similar to files except that tablets support modification right dynamic ones so invite your dynamic files and the template files in ansible is written through ginger to template language so I should be using ginger to template language right and the last is the vas vas they're also supposed to like store the variables but I told you that the variables are stored in the defaults right so then why we're so both where and the defaults store the variables right but the variables stored inside the vas have got higher priority so I can write here both vas and defaults stores variables but the only difference is that variables stored under vas has got higher prank and they are difficult to override whereas the variables inside the defaults have gotten lower priority so first you should understand these things what goes inside each folder default again I'll just repeat myself default variables lower priority static files will be going under files some trigger based actions like starting a service when the corn file changes will be going inside handlers meta information about your name we are address your email address information about the module or the role tasks the actual code sample a dynamic files having the extension of ginger - or j2 and vas again the variables but having higher priority and every if I go back to my tree here every folder will by default have a main dot ml file so the execution will start from main dot Yemen like if you but again I'm comparing it with puppet there you have got any dot P P so here you've got main document so let's start and build the code first now once we have understood the concept so what I do here let me first go inside the tasks so the actual code will be starting from the from the task so I move inside the tasks here the default file in main dot Yemen I'll use a more modular approach here instead of writing the code for installing configuring and starting the service in the one file I will again divide it so it will be much more easier for us to manage in case we have we need to do some troubleshooting aspects so what I do here I open my main Lord Yama here and I just write couple of lines here say I said say so I say include it's a different way of writing the code say Y Amal then I say include a second figure dot y ml and then I say include say service because the first thing that we have to do is that we need to install the package so that code will be here in this file not here but in this file then sending the con files sending HTML files will be inside the configure and the starting the service will be inside that service dot yam so I move out here now obviously I need to create those files so I don't have the files right so I will be creating a file install dot EML make sure you don't do any spelling errors here so obviously we'll be starting with three hyphens so I write the installing as to DVD package code right so I say here name say install Apache using the yum right and the name of the package is aesthetically and the state is latest so in case some updates are available they should be available I save and come out from here now I have got two files I will create one more file configured Ottoman so I open this same stuff so I am saying configuring a straight of recourse and sending sample html5 right so I write here name say s to DVD dot gone by time this name can be anything using the copy so the resource that I am going to use is a copy source is httpd.conf and destination you know the destination at ch2 DB d cone s - DVD calm now I should be copying this file under the files folder it will automatically get it from files and in case the con file changes so here I am calling the trigger when the trigger name is restart Apache service so I need to make sure that the name should be same and the next is I will create some copy some sample file the index dot HTML kind of stuff so send index dot HTML so I say copy SRC index dot HTML and the destination you know the default document root is very duck Duck Duck HTML indexed or testing that's all I save and come out from here I have got these files to the last file which is a dependency the service so I create a file service dot yam so it should be having the code for starting the service start Apache service so I usually name say starting Apache service ok so I should be using the service resource and the name of the service is obviously activity and states should be started so that it should be available across the reverbs let if I show you the folder and if I Katmai so these three files must be present so they are there so my task is being completed now what I need to do I need to step back and I need to have the files in this folder if I copy my local file here and I just do some modifications so let me just write here say coming from and Sibyl Apache rule so that we can be sure that my file is being copied so it's under at sea and Sibyl rolls Apache and files I also need a sample HTML file so let me create a HTML file also I'm not very good in HTML coding but I will just write a small code here h1 say learning and civil rules with napadas yes I break this yep and close it not a very difficult HTML quote but yes some code is there now what next so I have got these files inside my files now I will again step back handler need to be configured so I go to handlers and here the main dodge ml will all all always be there so use the same fine so right here name the name should be same now please start Apache service and action very simple service name is Esther DVD and state is restarted that's all now again I repeat myself the name the name inside the handlers should be same inside here notify this name should be same with the name of the task this should match okay so once I am done I will step back once again if I show you the just go to the tree apart a knot it should look something like this it's populated now so I got these static files which are going to be post handlers are specified it I didn't specify anything in meta so I can specify the things in meta if you want I can write about that on which distributions it is supported who is the author so let me just do that also and these files I had created I hadn't used any variables here because I am NOT going to use the templates in this video we'll be doing it later first understand the rules how the roles can be used so let me just go to Apache go inside meta and this is the file so I can write my stuff here author alok shrivastav all right description simple Apache role right and again your company name your informations as a netbook nuts which distributions it is available you can write all the things here on which platforms see here on which platforms that it will work so in case you want to give all this information so I can write here that name is e el Enterprise Linux and it supports more than five thousand six it will not support seven bit because it will support self second so I can write here seven or so so in this manner you can write a lot of things in save and come out again show you the tree this is my tree here that's how so if I try to run for the remote machine now the role is there the role is present but the point is that how will I sand it how will I use it so the main directory is your at sea and Sybil if you remember I had told you earlier also the main directory is your Xen stable so I can have a top level file like if you are comfortable with puppet we have got something known as site dot P P so what I can do and I can have a top level directory or sorry a top level file inside mine at C and say well is site dot y ml I can have this five site dot y ml and then inside the side dot camel I can just write host for it - I can - host so either I can use a specific machine or if I were a group of machines so in my case I will use example.com and then I can use the roles that which role you want to apply it it is very simple so I can just run it for the machine and we'll show you how it is done so I I just want to use it for this sir okay there's a - is missing here so rolls it's very simple Apache that's all that's all you do or if you have multiple roles this better to use it like this Apache if you've got more roles which need to be applied in say NTP keep on adding like this so I because I don't have an NTP here and just hash it out so save and come out and and and let me check the syntax and Sybil playbook side dot yeah Mel - - syntax - check often if there is any issues it will assume now if I move to ants to machine the machine which I intend to use if I do a rpm query has to do but it's not installed nothing is here fair enough so let's push the things from my server so I just remove this syntax check is gathering the facts I hope everything will work properly here installing the package you will see the role names the task name will be appearing here perfect it's installed now sending the corn file sending the HTML file and it's starting the service restarted the Apache service fair enough that's great so if I go back here and do the same command is installed that means guys if I go to the browser here and I hit my ants - I should be able to get the website wow learning and civil role with network maps that's great that's really good if I quit it arm down so and you can confirm all these things here if I go to where dub dub dub HTML the index traditional file is there if you say ashtray with corns as tribute cons this file is coming up here so in this manner you can use the roles to configure your multiple machines simultaneously now so would look at the beauty is that suppose one more web server is coming up so you just need to add the name here like this or if you have got you can also like this hose say a fake name say another server another web dot example calm and then you can write here rules Apache like this so the whole code will be reapplied again so that's the beauty of learning the roles in ansible are so I hope you have liked this video and in the next video I'll be covering the templates with a template language ginger tool so thank you for watching it keep learning keep smiling no matter what keep smiling keep learning thank you very much god bless take care bye bye you
Info
Channel: networknutsdotnet
Views: 92,190
Rating: 4.8402281 out of 5
Keywords: ansible roles, learning ansible, devops training, ansible videos, ansible, networknuts, alok srivastava, ansible apache role, devops videos
Id: SgxtxzQJM4w
Channel Id: undefined
Length: 28min 57sec (1737 seconds)
Published: Mon Oct 31 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.