Ansible Tutorial for Beginners | What is Ansible | Intellipaat

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello everyone welcome to this session on ansible. Today in this session I'm going to teach you guys what is ansible exactly and then move on to understand how we can create our own ansible playbooks. Alright so guys this is our agenda for this session I hope it's clear to you, so let's move on and start off for the first topic which is what is ansible? Ansible like puppet is just another configuration management tool and what is configuration management as we have already learned it's nothing but installing software's on multiple systems for example if you have a fleet of 150 or 200 servers and you want to do a particular software installed on all those servers if you do that manually that'll take a lot of time right and for that we needed something which could automate the installation of softwares on multiple system and the way forward was configuration management right and now what happens is any software that you want to install you can do that on a single click of a button on all the servers which exist in your fleet using the master slave configuration value on your master you will have the answer ball or puppet master installed and then on the slaves you just have to specify on what machine what to install and everything will be done automatically right this this basically solves two things the first thing is that the software's which were installed they were taking a lot of time if it were done manually and now with ansible or puppet in in the picture where everything has happened happening automatically it does not take that much time second thing is there is no room for error because everything happens automatically right so if there was any error which could be committed while installing software that is not the case now because everything is automated so that is why it you know configuration management as a concept became so popular and that is why so many tools have been developed and we have basically done puppet and today we gonna learn ansible which yet again just solves the configuration management problem right now some few facts about ansible so it was written by a person called Michael D Hahn right and the name ansible actually came from a fictional novel which was written by sulla Kayla Gwyn in 1966 right that's like 50 years before this novel was written and there was a communication device in this novel right and the name of that configuration device was ansible so it is the the name ansible has been taken from there by Michael Dion right and again this software was acquired in the Year 2015 by Red Hat so now any updates or any features that are Al to the software would basically be directed by the Red Hat company but in its true nature right now as well as simple software is open source and anybody can download it for free anybody can make changes to it because it is open source right so guys as we discussed that ansible playbook is the cool part of what ansible does in its day-to-day activity right so whatever you right inside the ansible playbook is actually executed on all the servers which are basically connected to the ansible server all right now how do you write the answer will play books you'll write it in the yamen language which is Gamal in markup language right this is the full form for that and as we move along I'm going to show you guys how you can start writing and ansible playbook right so that is as you will playbook let's focus on the word playbook first like what is the playbook a playbook means a book of plays right so a playbook essentially should have a lot of place so guys you have the first the first level in the tree of playbook comes a play right and you can have multiple players inside a playbook now what is a play a play is a combination of tasks that you want to do on a particular host right so you define a play and inside the play you define the tasks that has to be done and each task has something called as module it has something cool called as notify it has something called as handler and a lot of other things as well right we will discuss these things in detail when we will be talking about ansible rules for now let's just focus on the world playbook so like I said playbooks has a lot of place and each play has a lot of tasks right now let us see how we can define a play in a playbook so guys in a play you can have two or three tasks or four tasks or 10 tasks or any number of tasks written and these tasks are executed on a particular host using a particular condition right and that's why it's called a play what has to be played on a host this is one plate that is being played which is it is executing a command in host 1 and then it is also executing a script and Hosemann this is just an example although we will be doing this in this session but let us take an example that these are the two tasks which have to be done on a particular host so they can be combined inside one plate and then they can be executed similarly if I want to execute a script on who stew and install nginx one host to this would become plate to okay now how do you write or how would this particular playbook look like on screen this is how it will look like okay now how do these which are plays what are plays and what are tasks let's look at that one at one by one right so guys this particular block of code is one play where and you have to find the hosts right so whenever you define the host you start with a new line wherein you are defining the host that becomes the starting of your second play okay now the next thing is inside the play you have tasks so as you can see you have tasks over here and the first tasks that we have is execute command date this is the descriptive name of the task right so this does not get executed this could be any string that you want to give right second thing is you have to pass the command now what command has to be executed on the terminal that same exact command you have to mention here okay then the second name is execute script on server right so what is the script that you want to run test underscore script dot s H this is a script that I want to run so in this I have two tasks one is execute command date then Stauskas execute script on server every task will start with the name and has to be followed by a - ok - marks the beginning of a new task you specify the name of that task and then you specify how that task will be executed alright for a for example in this case there should be a command which is being executed in the next case there should be a script which has to be executed the syntax will remain like this right now whenever you're starting a new play in that case also you have to begin with the - you specify the host name that is this hosts : and then host one then whether that particular command or whether that particular play has to use sudo we mentioned that so if you want to use sudo in these commands all you have to do is type in sudo colon and then say yes okay then you give the name of the play so the name of the play it could be anything like I said it's just a string you can pass in anything over here and once you've specified all these info you start with the tasks right so this is play one guys similarly now if you have to write played - I'll have to begin with hosts again which I have done over here now where I want to run these run this particular play I want to run it on whose - what is the name that I want to give this play I am calling it played - do I want the sudo to run yes I want this will go to that then modern tasks that I am executing in this play I want to execute script on server this is the name it could be anything and then I specify this script which has to run on that particular host then I want to install a particular package so I'll say the name of the task is install nginx and the task that I have to do is install a ssin so that's why a PT is written over here so when you are dealing with Ubuntu systems you'll have to type in a PT over here if you're dealing with sent OS or Amazon Linux it will the command over here would be yum okay so apt : and then the package name which is nginx that I want to install and the state is latest so over here you basically specify the version that you want to install so install the latest version so this is how you write a plate now let us summarize what we just discussed so we missed out on this thing that we always start a Yama file with three hyphens right so evenly anytime when you are starting with the ML file specify these three hyphens and only then begin with your playbook so I start with specifying these three hyphens next thing I do is that I have to create a play so I for creating a play I have to start with a hyphen and the next thing that I have to specify our hosts right and the name that you're specifying for the hosts over here has to exactly match with the slash etc' slash ansible whose slash hosts file so whatever IP address you have mapped to the host name over there right that host name only you will be using over here you cannot use some some other host name okay once you have specified that the next step was to specify the tasks so each play is like a dictionary and has name whose tasks and the order doesn't matter right so you have the name you have the host you have the tasks so it becomes a dictionary so it has all these information each play and once you define that and execute that then it executes according to what is asked inside that particular play alright so when you are writing a playbook that becomes a list of dictionaries so if this is one dictionary where you have hosts name and tasks and this is a mother dictionary where you have the separate version of hostname and tasks when you combine them together inside a playbook it become a list of text are you okay so again seeing what tasks are tasks are nothing but they are basically commands that we want or tasks or I can say things that we want to do on the remote server from the ansible master those are termed as tasks then the next step would be to basically creating a playbook how can we create a playbook so first let me just move on to my ansible master so guys this is my handsome master what I want to do is I create a directory called ansible ok so this is my directory now I'll go inside ansible this is my in simple directory guys now the first thing that I will do is I create a playbook ok so let's create a play book called sudo nano lay dot yeah ok this is my playbook now I want to create a playbook wherein I will be installing the apache software on my host 1 which i defined in my hosts file now how can I do that the first step that you have to do in creating ansible playbook is define these three hyphens once you have done that now you have to start with the play how can you create a play start with a hyphen and specify the hosts so I want to do this particular play on host 1 ok then do I want sudo yes I want sudo the name of this play let's call it lay 1 okay and now let's specify the tasks ok now inside the task guys the first thing that you would do is you'll have to define a task so let's name the task something so let's call it install Apache ok let's call it this then what I'm gonna do is I'm gonna give the package name EPT and let's say the so if you want to see the syntax for that the syntax is actually available here so apt and then you specify the name of the package that you wanna install so the name would be Apache - and the state is going to be latest ok this is all you have to do when you are creating a plane all right let's start by this let's see if this is working or not so my play is now ready right let's check if I can ping my host one sensible - am ping host 1 so it says could not match supplied host pattern ok so I think that's because the name is slave 1 yes so the name is Levon so let's change that in our Yama file as well so sudo nano play dot Yama and the host name is slave 1 okay now let's try to run this playbook now how can I run this playbook type in and civil - lay book space play dot gamble once you specify this this playbook will start executing now before I execute this playbook guys let me quickly show you on my management console that if I go to slave 1 right now and if I go to this IP address nothing is visible over here right it says the site can't be reached ok now let me come back to my master I'll execute this playbook so now it is installing Apache it will take some time rise because software installations take some time so you can expect around 15 to 20 seconds it will take to basically install or do this particular task and once this task is done what I can do is so as you can see it says task the change has been successfully done and ok is equal to to changed equal to 1 and nothing has been failed now if I want to do this all I have to do is come back here refresh it and as you can see the apache software is now successfully installed on my slave 1 awesome so i have successfully done something on a remote system right which is this so now what i'm going to do is let me just uninstall apache sudo apt-get purge apache - i just uninstall the package and i'll tell you why i'm doing that so my package is being uninstalled now if i do a refresh here you can see i can again i again not be able to reach to this particular so now what I want to do is I want to add one more host to my ansible cluster so let us do that let us launch an instance it has to be an Ubuntu instance and the processor is T 2 dot micro security group I select an existing one review and launch launched it with my key pair and now it's ready okay so my server is being launched let's call it ansible 3-1 okay and now if I go to my running instances I can see that a new instance is now being launched now what is the IP address of this instance it's this list so let's copy this and launch a put T terminal let's select the color to be white so that we can differentiate it easily okay let's click on open and now let's login to our instance alright so successfully logged in now how do you add a host all you have to do is update the machine sudo apt-get update and the next step would be to install Python sudo apt-get install Python once this is done I just have to go back to my master I'll have to do make the key less SSH possible and for that I'll have to do and as CD into the SSH directory and then I'll have to do a cat on ID underscore RSA okay this is the code that I will be copying on my slave - okay so this must leave two guys now I will be going inside the SSH directory and I will be going inside the authorized key file and now let's add this entry over here save the file and now let's try to SSH from over here SSH one two at the rate this is the IP address that I'm gonna connect on and yes I am able to login to my slave so to verify this is the IP address once every two thirty one twenty nine thirty four and if I check it over here it's 170 231 29:34 awesome so let us remove this so let us exit the SSH connection and now I will have to add this host in the ansible post-fire so in order to do that I'll have to do one nano on /et c / ansible slash hosts okay and now let's just scroll down and add one more slave over here with this name and the bill underscore ssh underscore host is equal to this IP address that is of the slave I'll add this I'll save the file and now I Purdue ansible - M ping all I should get two servers ready over here okay awesome now why did I do this all right so now let let us go to inside ansible right and let's try to edit our play book let's try to add one more play inside this so I'll add a play and let's say I am doing it for slave - okay I want sudo to be there I want the name of the place should be laid - okay the tasks that I wanna execute as follows so the first of all the name of the task would be to install let's say I'm installing Apache on this system and I want to install nginx on my slave 1 ok so let's change all the things accordingly so I'm installing nginx on my slave 1 now and I'm installing the Apache on my slave - okay so name is equal to Apache 2 state is equal to latest alright seems cool now I will be executing this playbook and let's see how it goes so basically with one command what we are doing is we're installing software on two different systems right and the software that we installing are also different right so this is the IP address for my slave - so as you can see if and if I'm going to the IP address I can't reach anywhere similarly if I'm going to this IP address I can't reach anywhere okay now let's come back to our monster alright now let me execute this command so it is ansible - playbook and then play dot yeah okay so let's execute this so it is executing play one it is installing nginx on my slave 1 alright ok so it has been done now we are securing plate to and I'm installing a patch it so as you can clearly see right now I show you the browser my server one is already ready and it says welcome to nginx awesome and my slave 2 is also done so if I go here and if i refresh I can see I can see the Apache page alright so I have configured two different servers from my master right addict click of one button right I just executed one play book which configured to servers of mine installed two different software's on it based on the name that I specified all right so awesome guys we I think we have almost begin we are we are almost a beginner now with the playbooks I'm sure you guys can also write your playbooks but let's take it a notch up let's try to do something else as well now if you have to execute scripts on remote system how can you do that okay so in order to do that all you have to do is you will have to create a script first in the same directory where your PlayBook is present so I am a playbook present in this directory that is ansible right so in this let me create a script so let's say I create a script called hello dot SH and what I want to do in this particular cribbed is I want to create a file let's call it let the content be HelloWorld for that particular file and I want to put it in slash var slash w w w / HTML okay I want to put it over here let's see how it goes so and the file name has to be one dot HTML so in this case what is going to happen is that I'm gonna execute this on slave to where my apache is basically installed and that should basically create a website automatically and if I go to this particular IP address and I go to it / 1 dot HTML if I hit enter it should basically show me the web page over here let's see how we can do that so I have created this script guys so let's save this script and now let's try to make it an executable file so let me make it executable so I am just changing the permissions for this file right and this has to run n sudo alright so my script is now ready now I'll go inside my playbook guys so I will go to sudo nano play God yeah Mel and I want to do this task on my apache so right so i am inside my apache play and i am adding a new task now which is adding website ok and what i want to do is i want to run that script so I'll type in script colon and what is the name of the script hello dot s that's all I have to do now let's save this play and now let's try to run this place so I do a ansible - labor and I have murdered on this play okay let's see how it goes nginx is installed okay so on slave 1 everything was executed everything is same so it says ok equal to 2 change the equal to 0 on my slave - there is one file that got changed now let's try to do a refresh over here can you see automatically a website was created which is now called hello world now let me show you one more awesome thing over here now I'm not going to any of those servers right this is my slave - I'm not going over here I'm still on my master now what I can do is I can just simply change the script file so if I come in this great fight we say I go inside the script file I change the text to hello world one two okay mind you I'm not going to any other so I am just running the same command again right again the commands we run as you can see this says on slave - this one change and if i refresh it now can you see the website has changed automatically right and this has changed without me going on to this server and this is the power of configuration management guys now imagine you have hundred servers and you have to change a file in all those hundreds hours how do you do that it is impossible to do it manually I am saying it is impossible because time is of the essence when you are working in businesses right so you can do it using ansible in this kind of way right now let us come back to our slide guys so if I come back to my slide all right so now we created a first playbook in this particular example also they're basically running a command and then they are running a script so we did similar thing right but when you are executing a command basically you cannot see the output that's why I change this particular demo but you can also try this demo also so the syntax is clearly visible over here you can just write the syntax and I am will file and then try to execute it and see how it works for you you can also create a demo for your own self as well and see how things work all right so we created the first playbook using this particular command and we created this shell file or the script file like this right and then we change the permissions for that script once we once everything was ready we executed the ansible playbook and the script file will automatically taken up and shifted or basically copied to the slave that I want to execute it on and then my playbook was basically executed all right so guys this is how you create a playbook so thank you guys for attending today's session I hope you have learned something new today so see you again in the next session goodbye and have a great day
Info
Channel: Intellipaat
Views: 41,289
Rating: 4.8543048 out of 5
Keywords: ansible tutorial for beginners, ansible tutorial, what is ansible, ansible training, ansible playbook, ansible, devops tutorial, ansible tower, devops tools, ansible roles, devops, devops tutorial for beginners, ansible playbook tutorial, devops edureka, ansible modules, ansible training videos, infrastructure as code ansible, ansible vs chef vs puppet vs salt, edureka, Simplilearn, Intellipaat, DevOps Training, Devops Videos
Id: YI48bKykx7k
Channel Id: undefined
Length: 25min 59sec (1559 seconds)
Published: Tue Jun 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.