Ansible for beginners -- Complete end-to-end tutorial video with practical solution #ansible #2hrs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone hi uh this is Rohit Singh and welcome to my channel and welcome to my new web series on nsibil so basically in this video tutorial we'll talk about what exactly sensible will set a vegetable from scratch we'll talk about ansible imperative command those are adopt commands we'll talk about what exactly sensible modules then we'll talk about how do we write playbooks from scratch and followed well we'll talk about ansible variables and then we'll talk about ansible rows how do we create custom rules in ansible that is what will focus on more and then after at the very end I'll show you how do we integrate ansible with azure devops so what are the things how do we create the private and public key how do we create the service connection how do we execute ansible Playbook using pipelines that is something we'll focus on so basically we'll cover each and everything which will get you started and which will get you running with ansible so that's that's all about this course guys this is what we'll do in this entire ansible course so let's get started let's start with the first session on ansible let's understand what exactly is [Music] hello welcome to my channel devops Mela this is Rohit Singh and welcome to my ansible web series so basically I'll be covering everything about ansible in this video tutorial guys so let's in this video in this particular video we'll talk about what exactly is ansible why do we need ansible nowadays and we'll set up ansible on Azure VM on a Ubuntu OS okay let's get started and let's see what exactly is ansible okay firstly why do we need ansible ansible is basically required for provisioning that can help you with provisioning it can help you with configuration management it can also help you with continuous delivery application deployment security compliances so let me give an example nowadays like like you got n number of VMS and on those n number of VMS you keep on doing patching you keep on making configuration changes you keep on making and number of things just imagine if you got 50 VMS logging into each and every VM and running those scripts or running those commands it's it is a very tedious job that is where ansible comes in picture ansible can help you automate this entire process from one single location you can run through ansible ad hoc commands you can run through ansible playbooks and you can execute all these commands whatever you want you can also do provisioning uh even though that is something I'll not suggest because we got dedicated provisioning tools in the market like terraform AR and clock formation which does pretty much a very good job so it's better stick with those tools if you want to provisionally and civil is basically used as a configuration management tool primarily okay now many of you will say why can't we do the same thing using some scripting language okay definitely you can do it I'm not denying if you have that time to write those scripts because if you know ansible is playbooks are written in yaml that's very easy to understand written in very simple English language if anyone doesn't know coding they don't come from any coding background just read by reading through the Playbook they'll come to know exactly what's happening that easy it is wherein on the other side you should have a coding skill set firstly to understand what exactly is written and if you want to write the code the script you need to have those skills plus you need to maintain those scripts okay wherein ansible is agentless what do I mean by agendas that is something I'll talk okay so ansible becomes very simple powerful and agentless agentless is basically your target machine there's no service or no process anything is running no Edge and no ansible plugin running on the end server the only thing NC will require is SSH connection and if you do successfully can whatever task you want okay that is what endsable for you now as I said ansible is a configuration management deployment and orchestration Tool so you can orchestrate a task in a playable and based on whatever you have defined in the Playbook now what exactly is Playbook and all that is something we'll talk about when we come to the playbooks section in the later part okay so basically it will go and execute the sequence of tasks like for example if you if you want to know what are the sequence of tasks like suppose installing Apache or doing Apache web server configuration restarting Apache server these are the sequence of tasks which I'm talking about it is post-based configuration management to post page because you this ansible has a management note this consider this as a VMware you have set a message and these are all your target machines okay okay so this connects using SSH and civil connects using SSH you have the Playbook over here the Playbook has all your sequence tasks like installing Apache restarting Apache making configuration changes Apache you have those tasks defined inside the Playbook and inside the inventory file there are two important files which you have on your ansible server basically where you have ansible installed so this is the only server where you will have ansible install all other servers will be connected using SSH there's no agent running on any of these ad servers in the inventory file you will Define the hostname and the IP address over here now what exactly is inventory and everything we are covering in details coming session so I'll leave leave it over here for now okay so ansible can automate your entire ID infrastructure by providing you large productivity it can help you automate your app complete ID infrastructure from the management perspective from the deployment perspective from managing your VMS making configuration changes installing packages so there are a number of things what ansible can do so what we'll do we will go back to Azure cloud and we'll settle set up ansible from scratch now let me go I'm already signed into Azure Cloud right now let me go to my cloud and I can go and create a nsip a VM over here now why exactly I'm creating on cloud because I have a count on Azure flow that's that's over here if you are new to Azure Cloud you can sign in and you can take advantage of free tier account and you can create this VM I'll be using a free tier VM over here guys so if you fall in in the free tier uh you will not get charged anything but if you don't have free tier account don't worry the BM which I'll be using it has very minimal cost okay if you have AWS you can go and create a VM on AWS it works the same way Google account you can go and create a Google account if you have Oracle uh set up on your local system itself and if you have Ubuntu VM inside it you can use that even so I just need a VM wherein we can set up ansible that's it there's no hard and fast rule that you need to have Azure AWS okay whichever whichever place you have a VM and if you can set up N7 on top of it that's valuable okay let me go and create a VM this is a resource Group I'll mix and I'll just name it as ansible VM and I'm using Ubuntu Server 2004 guys just keep that in mind and this is the size this false and the free tier I'm using One V CPU and one gig of memory that is more than enough for ansible to run but later when you're using in real time you may have to have a bigger configuration this is just for testing so I'm using a very small configuration let's click create a user ID password okay let me open port 22 and Port 80 and let you know why exactly I'm opening a port 80 and 22 22 is for connection connecting to the VM Port 80 because I'll be setting Apache web server and later I'll be I'll be using that web server in my I'll be deploying the web server using ansible Playbook and ansible network commands okay so let's come back while this VM is getting created let's come back and once this VM is created okay guys if you see my VM is created let's quickly go and see so I got the sensible VM created we need to go and log in so I'll use my public IP over here and I'll use git bash I already have git bash install on my local you can also use uh dos CMD which whatever you like if you want to use put T you can even use Putty for login git bash is something I like more so let's go and use this kit bash it's nothing but it's just a bash shell which I'm using guys okay hope this is visible now let's go and use it this is my user ID followed by the public IP perfect let me quickly login perfect so I'm currently logged into my account let's switch to sudo okay I'm logged in as a sudo user so as if now I don't have ansible install on top of it so I'll go and install in symbol so let me update the local repo first so let's go and set up ansible on this VM so for testing purpose guys for as we are just learning things so I'll not have multiple VMS I'll be using this particular VM itself as my master and a worker node okay so I'll be setting up things using ansible on this node itself later in later in the course when I'll do ansible integration with Azure devops at that time I'll show you how do we set up multiple VMS and how do we configure multiple VMS using one master node okay for now just to save uh money because if I want to set up more VMS I need to go and launch more VMS okay I'll show you where exactly changes needs to be done if you want to have more VMS added to this master load okay don't worry about that so let's go and install ansible this is the step I'll share the steps with you guys you can find the steps in my description if anything is missing you can just let me know in the comment box okay please do let me in comment if you want to add some more sensible topics anything as such please do let me know in the comment box okay guys thank you now pause this we'll simply go and install ansible okay ansible uses python in the back end so there will be python dependencies also which will get downloaded while you're downloading and simple okay keep that in mind let's wait while ansible is getting downloaded on this particular VM guys ansible is installed successfully now let's go and see let's verify it by running this command ansible hyphen hyphen version perfect so we got ansible install 2.12.10 successfully installed this is the location where my config files are so let's go and see this location first let's go to Etc ansible these are the file and civil.config Host this is where your inventory files will come in this is the default location if you want to have a custom location we can do that also and this is where your rules what exactly sensible rules that is something we'll talk about in a greater details for now let's go and see this particular config file okay by default now you cannot see the config if you want to any so this is by default disabled so what we can do we can run this command and we can have the config file visible let's run this command okay now if you go and see you will have the ansible config file over here see this you have the config file I'll come to this config file there are a number of things which we need to do so we'll come to this section again now next part is let me introduce you to ansible inventory okay we got the ansible setup done we know what is ansible we have done the ansible setup now let's go and see what exactly is ansible inventory okay let's go back to PPT ansible inventory is defines the managed nodes to automate so basically the end server the host machine on which you want to configure something that comes inside your inventory file so okay your on your ansible master node the inventory file resides and you will have those end server details added to the inventory file it can be added as a group so you can automate tasks on multiple hosts at the same time ultimate as a group means if you have a list of DB servers okay and you want to do some sort of changes on the DB servers so you can have a DB group and you can all add all those end servers inside that group same way if you have if you want to have something like a web server okay like if you want to have Apache web server and you want to set up Apache on all those servers so you can add them as a group you can add them all as an individual also okay if you have some individual server in my case I just got individual one single server so we'll we can add it as a group also we can add it as an individual server also okay this is the place where you're by default inventory file resides now it's not necessary that you need to call the inventory file from this location itself but if you're not defining any location then it will go and read through this particular location itself okay keep that in mind these are few parameters like if you want to ansible can connect using SSH winrm localhost so you can mention if you want to have some connection mentioned inside the inventory file even that is possible if you want to Define support by default it is Port 22 if you want to have some custom ports Define you can do it you can also Define the user root administrator you can also have ansible passwords defined this is not the right way of defining the password guys that that is not recommended but yeah for timing while we are learning we can have the password defined later if you want you can even integrate Vault okay keywords wherein you can have all this added the passwords and everything added okay make sense and Linux we can connect using SSH while in Windows we can use Powershell remoting these are the two methods you can connect to Linux and Windows machine using ansible okay perfect now let's go and do the first inventory setup let's go and add come to your VM now I'll be using the default location for now later I'll show you how do we have a custom inventory location that is something we'll see later for now let's stick with the default location Etc ansible host this is the file now you can get more information ungrouped like if you if you want to have ungrouped end servers you can do it this way if you want to group it this is how you do it the sample is given over here there are more samples there are more simple this is I'm using a VI editor if you want to have something else as a Host range like if you have some DB numbers like DB one two three something like this so you can also Define The Host range like this okay let's go and do the first inventory setup so I'll I just have one single machine so that is my master machine on that Master machine itself I'm I'm doing my ansible run so basically I'm setting things on on my master machine itself so I'll stick with localhost for now just imagine if you have other VMS if you have created one more VM okay so you can put the VM public IP over here or the host name or the domain name depending upon what you have so whatever process I do it's exactly the same process you need to follow if you have n number of VMS guys nothing nothing different nothing alien nothing the only thing will change is this localhost will change with public IP or the domain name whatever you have okay now over here I can Define the ansible user so the user is nothing but my user through which I have created this account so what is the username we got we got ansible user devops we have created this VM using devops Miller I can also have the password defined over here but I'll not do the do the password part right now over here that is something we'll do later for now I'll just Define it like this so I have my Local Host and I am ansible user defined over here now let's go and do a quick check so I'll run some ad hoc command to see if I'm able to connect to my local host or not how do we do a how do we do the quick check I can run a ansible ad hoc command so there are two ways guys you can Implement an ansible one is through Adobe command that is imperative way the second is declarative wherein you declare you write a playbook in yaml so first we'll focus on ad hoc commands we'll see what are the different set of ad hoc commands okay and we see we will see the importance of ad hoc command why do we run ad hoc commands and things like that for now I have defined something in my inventory file and I am testing if everything is looking good how do we do it I can run this command a simple command n symbol spelling mistake and symbol followed by hyphen I no hyperi is not required followed by all so I have only one single in end server edit in my inventory so that's why I'm defining as all all is basically it will run all the whatever commands you're executing it will get executed on all the end servers whatever is there in inside your inventory file as if now I'm just doing all if you have any group or anything as such you can call that group also over here okay for now I'll stick with all okay all and hyphen M hyphen m stands for module so the Rand number of modules in ansible we'll talk about that later for now I'm just running a ping module let's make it yes okay I'm getting some error error is failed to connect to host via SSS permanent added localhost list of known host unreachable permission denied this is because we are not mentioning the password guys okay I am not doing SSH this is I have not I'm not using the public private key thing right now over here and I have mentioned my username inside my inventory file but I have not defined the password okay so for that what I can do I can Define that I can pass in the password over here Itself by a flag call hyphen k so it will prompt me when I run the command now it will prompt me for the password okay let's go and put in the password perfect now do you see I'm getting a ping pong that means we are ready to go I can make whatever whatever sort of configuration I want to do like if I want to set up Apache nginx what do I want to do I can go ahead and do it okay so that's it from ansible setup guys in the next part we'll see what is ansible ad hoc command what are the different modules we have okay so stay tuned and if there's anything for me please do let me know put in your thoughts in your comment if you want to me to add something delete something please put in your thoughts in the comment okay thank you bye hey everyone welcome to my channel devops Mela so this is what we did in the last video we have set up ansible we have setup inventory and this is the testing what we did by ping pinging ponging to the Target machine everything looks good now taking this forward will understand what is ansible ad hoc and what are the different modules we can use in ansible okay now let's understand there are two ways as I said earlier you can manage and work with ansible one is imperative way that is what we'll focus on in today's video and the second one is the declarative way that is something we'll talk about in the later video imperative way is like running ad hoc commands guys on request so if you want to if you want to execute something without running writing a Playbook or anything as such you can write this imperative commands Okay like for example one of the example is this we ran the Ping command so imperative uses two things one is module so there are what exactly is module we understand that now one is module that is something we Define it as hyphen hyphen M the other is argument the one what exactly you want to do for example I'll show you one of the module I can have something like ansible all hyphen M I can use shell module so basically shell module will provide a shell to you and you can execute whatever commands you want so in with shell module I want to pass in an argument that I want to know the free memory on my target machine okay so what is my target machine and my inventory file we have already defined the target machine as my Local Host okay later you want to add some other machines to it you can simply replace the Local Host with a public IP or a domain name okay hyphen K because I need to pass in the password now let's put in the password so see I'm getting the output this is what shell module and this is the argument which I'm passing single inverted comma then this is what I'm passing that this command needs to be executed so that's why this is in single inverted comma okay so this is how it looks like now I'm fed up guys of passing this password again and again so what I can do I can go to my inventory file again and I can make some changes over there ATC and symbol host and what I'll do I have something defined as ansible SSH pass and then put in the password over here so this is again I'm telling guys this is not the right way of doing things this is not the right way of doing things give me one quick minute I'm getting some extra characters over here so what is the right way you can have some keyword integration done as if no I cannot talk about key Vault over here because it's a very initial session of and simple but yeah maybe in future then just put in your comments that if you want to have something related to key Vault okay because this is we are covering very basic offensible covering keyword will be out of out of scope okay now let's go and run this command again we remove this hyphen K and see if it works perfect this is what I wanted now let's go and see what these modules are before we jump on and see all different set of modules okay ansible modules are Standalone scripts basically that can be used inside ansible Playbook so when you write the Playbook these are the modules which you will be calling inside the Playbook also guys okay and in ad hoc commands Okay so there are module system related modules like if you want to have something related Quran if you want something related firewall D hostname those modules are there there are command modules like remove standard input creates there are file modules we'll see this file module also copy file replace we got database module we got Cloud module we got Windows module we got and there are many so these are the modules these are Standalone scripts which is already created by ansible these modules using this module can easily manage the configuration and there are many things you can do with this module I've just given few example of this modules if you go to the official website of ansible this is the complete list of modules guys there are so many things you can do with ansible like for example if you go to Cloud module again Cloud 2 you can connect to the cloud services like AWS Amazon and many things okay Azure you can even connect to Azure if you come down you will find Azure over here at the bottom these are always easy to relate things where the scroller see you got ajar over here so there are so many hereku Hawaii so there's so many Cloud modules also you can connect with okay keep that in mind now these are few example I'm giving to you this is not it's not possible for me to cover all these modules guys depending upon your requirement you can come and look into this documentation so this is file module we can even see what this copy module is how do we create files and things like that that is something we can go and see now we have seen the cell module now let me show you something related with file module okay now imperative command as I was talking about what is the importance of using imperative command this is basically used by administrators if they want to do something very quickly without writing a Playbook they can go and do it immediately by running some imperative commands uh this is not good for production guys definitely if you want to do something in production you need to you should write Playbook you should have configuration written but yeah if you want to do some sort of monitoring task like checking memory checking CPU creating some files you know small small task if you want to do without writing Playbook so definitely you can make use of this adoc imperative commands Okay the disadvantage of this imperative commands like if you run this command if you log out of this terminal no one will come to know what command was executed okay so that is the biggest disadvantage so this is only used for small purpose like small small things if you want to do not for production definitely not nothing to do with production related activities okay now let me show you one more module okay I'm fed up of writing the sensible again and again so what I'll do I'll set up a Elias and I name it as a okay so I'll just set up a now let me see if this a works perfect now in place of ansible I'll just write a now okay okay let's go and runs few more ansible commands and symbol ad hoc command so let's put it a all uh let's use the module shell again and in this let's do one thing let's create a file let's name that file as let's create this in the home directory and name it as index Dot HTML okay perfect it's created how do we verify I can make use of shell module itself and I can quickly run LS command unless at home or devops Miller okay let's see this perfect index.html so this is what shell module does for you okay now I can do LS hyphen L as well for long listing and I can get this is read write access which has been given if you want to modify this axis okay if you want to have some modification done with the access then we can also use file module for that I can have specific modules such as file let's go and change this module to file I'll have the argument but this time I'll not have such in a single code I'll have argument as double quotes because we need to define the path as a destination so the path is home devops mailer index dot HTML and let's give modify the permission to 600. okay read write only for the user or to the owner rest all zero zero failed to connect the host via SSH host unreachable that's weird now let me try it again done okay this time if you see we have changed it six zero zero index.html file now let me go and do LS hyphen LL this is how it looks like now if you want to change this uh user and the group the person who has created the file and the person and the group sorry it's owner and the group if you want to change it then even that can be changed using file module okay if you want to change the owner and the group you can even change that using file module so let's go and see this now with Mod if you want to add something more like owner you want to change owner I want to change it to root and I want to change what what else I want to change I want to change the group let's change the group to root itself okay what we are doing we are changing the index.html file modifying the permission and changing the ownership and the group permission group okay let's see this if we are getting any error perfect I'm getting some sort of a error that's change permission ownership operation not permitted why because you need to give pseudo privileges correct guys can we change without a pseudo privileges can we directly run and change the ownership and the group of any particular file the answer is no so what we need to do we need to go ahead and give pseudo privileges so how do we give pseudo temporary pseudo-privileges by just adding one flag hyphen B so become as pseudo so that is I'm we are giving escalated privileges with this hyphen B now done this perfect you see it got changed same way if you want to have some directory created a new directory created you can use the file module itself and if you want to delete the directory you can even use the file module to create and delete the only additional thing you need to add let me just show you and let me remove all this mod and all the only thing I'll keep is the home devops mailer and let me create a directory called test okay the only thing I need to add is State equals to directory this is I need to define the state that it's a directory now let's go and see if the direct is getting created perfect now if you see there's a test directory got created okay if you want to delete this again file module the only thing is change the state to absent now this is all you don't need to remember guys all these flags and states you don't need to remember now what I'm exactly if it's if you see this this is deleted now if you come back to this documentation which model we are in let's go back and open the file module see this file module come back in the file module what all things we have used owner see this owner we have used we have defined the path and state app send directory file hard link touch all this can be easily done see the touch can also I I have created a file using touch using shell module now the same thing you can do it with file module also so it's not necessary that one you you cannot use the shell module or you can also there's a there's one more module called Command Module so these are very frequently used modules which you can go and use it okay now very important module package module let me show you some package module then we'll move on before that let me exit out of this and let's go and verify the host now where exactly I am home devops Miller okay and this is where I have the index.html file and this is where the directory was created which we later deleted now let me edit this index.html file and add something to it I'm adding a very simple HTML tags to it welcome to devops mailer and Sable course okay why exactly I'm adding you'll understand this in a bit okay perfect this is perfect perfect now let me open this file and verify once welcome to devops will ansible course perfect okay let's go back to sudo now why exactly I've added this that is something we'll understand later but for now let me introduce one more module called package module like for example you want to install Apache on your end server how do you install Apache now on Ubuntu how do we install if you want to install Apache what all things you need to do you need to go and run the command app apt install this is the command you need to run correct install Apache 2 something like this you need to do how do you do the same thing with ansible okay with ansible I can run a simple command and symbol all and don't forget to give root privilege escalated privilege because without the without it you cannot install any package on any machine so that needs to be given hyphen m i will be using apt module in this yapt module because this is my Ubuntu machine what if it is my Centos or something else then you need to use send a yum okay that is both are supported by ansible and then I'll pass in the name of the package what is the name of the package Apache to and there's a state we need to pass in the state the state is present present will make sure that the module is present on your end server now what is this let me see if I am not doing any typo perfect looks good okay I did exit out let me write an symbol completely now let's wait and if you want to know more like what like while we are waiting what's happening then you can use a flag hyphen V for verbose which will give you a detail in description what's happening if you want more details for debugging purpose then you can use hyphen vvv triple V okay now this is done now this particular Apache is successfully installed how do we verify I can run shell command or I can run any sort of commands and I can see if Apache is successfully installed or not but because it is on my Local Host I can even simply run this system CTL status Apache to to see if it is installed see it's installed but this is because I am doing everything on a local host guys that's why I can run this command if you have any remote host okay in that case you can use shell command or shell module or you can use Command Module for do the for doing the exact exact same thing okay so let's verify if Apache is installed working http perfect I can see Apache coming up now remember we got some index file created can we put in the index file in the right place so that my index file will be visible whatever we have it in the index file can we do that how do we copy the configuration so basically I'm showing you using a DOT command how do we copy the configuration okay so let's run the command and symbol all again uh hyphen M let's use a copy module hyphen a and then let's pass on the values so where exactly the index file is the index file is kept somewhere in home not this way let it be home devops Mila that's why I've updated the index files guys index.html now what is the destination destination is the file should be residing in where triple w dot HTML this is a place where the file should decide okay so what I am doing exactly and changing the default index.html page of Apache with my own page so basically I'm showing you demonstrating how do we configure using ansible let's wait not writeable so we need to give escalated privilege perfect now let's verify if things are looking good how do we verify refresh this perfect welcome to devops Mela ansible course now this is what exactly we did using ansible guys we ran through ansible command we have installed Apache on the Local Host we have copied the configuration of the localhost now don't get confused that this is happening on the localhost the same thing exact same thing you can do it on other machines just go and change the inventory to with the right IP and the username and the password that's it and to run the exact same command nothing else this is I'm using one single machine just for testing because even you guys will practice unnecessary launching multiple VMS I don't want you guys to do that so that's why I'm just using one single machine okay that's it from Edo come on guys please do let me know your thoughts in the comment and do let me know if you want to cover any other topic in the next part I'll cover Play books that is quite Advanced so this is the same thing exact same thing what we're doing and that is something I'll try using Playbook okay that's it for me thank you very much bye bye hello all welcome back and welcome to my ansible web series so in my previous video we spoke about what exactly is ad hoc command we saw how exactly we can work with ad hoc commands that is something we have already seen it going forward let's see this was imperative way of working with ansible now let's go back and see how do we work with declarative approach declarative is basically writing configuration declaring whatever task you want to perform declaring it into a yaml file so ansible supports yaml based configuration that is how you will be declaring everything this is how ansible will help you orchestrate all your tasks okay using a Playbook now what exactly is a Playbook Playbook is nothing but it's a list of tasks that automatically execute against host so we were executing the task now like if you go back and see my previous video we were executing the task using ad hoc command okay now the only difference is that ad hoc command is one time if you if you're doing something very repetitive you very frequently then add a command will not make any sense so it's better to use Playbook okay so what is Playbook A playbook is nothing but it's a single yaml file a play it has n number of plays so the plays are nothing but set of activities whatever you want to do known as task to run on the host what are the tasks and action do we perform on the host whatever action you want to perform like executing a command running a script script installing a package shut down restart these are few actions which you want to perform that is what a Playbook does this is a classic format of Playbook guide this is how a Playbook is written I'll I'll be writing this Playbook from scratch now I'll show you how exactly in a very easy manner how a Playbook is written secondly this is how once you have a Playbook ready this is how you will be executing a play perfect now let's go and let's write the Playbook and let's see how exactly it is written and for this time I thought of having one more VM guys so I have created one more VM I've named this VM as ansible host so whatever demo we were doing till now that I was doing on a local host I thought of adding one more VMS to give you a complete idea how exactly in real time things works now if you see this I got this host machine I have the public eye IP I have not configured the DNS but if I want I can do that even but for now it's not required now if you see I got two VMS I have one master which on which my ansible is installed and I got my host machine so let me just log into the host machine for now okay if you want to login this is not even required it's okay let's go and update the host file and add this okay so we are using localhost uh now this time let me go and use a web server or I can create my own group or I can just use this one I mean that is fine okay let me just put in the information such as um move this let me copy and paste the IP again I'm getting some extra character okay now ansible user let me Define the user that is devops Miller ansible password again this is not the right way to insert password guys the better way is to integrate vault for now let me into this web servers okay now let me quickly go and check if things are working so ansible I'll do a ping and symbol web servers module ping okay so I'm getting some error there is using SSH password instead of a key not possible because host key checking is enabled and then and does not support SSH pass please add the fingerprint in the known host so one one thing I can do I can do SSH to this machine then this error will be gone one way so I'll just show you that SSH today warp's mailer and what is the public IP of this host machine this is one way so what will happen the known host the fingerprint will be added to the server and server known host and you'll be able to do SSH after this see this the fingerprint is getting added right now this is one way of doing if you're using passwords guys if you're using private public key then this is not even required okay this is done now this is one way let me do a ping again perfect this looks good but if you want to disable this host key checking even that is not recommended again but for now if you want to disable it you can go and into ansible configuration okay and look for host key check um wait a minute okay where I am logged in right now yeah this is good let's go back to this drive again Etc and symbol let's look into this configuration what exactly is this data and not okay let me have the example config file first now I can go in look for host key now if you see the host key checking is set to true and it is disabled right now what I can do I can enable this and I can set this to false so I'll not get this error okay okay so this is the setup is done I am able to run my ping I'm able to connect to the end server everything is looking good perfect now let's go and write a Playbook for writing a Playbook what I'll do I'll have some folder created I'll name this folder as Playbook and I'll use Visual Studio code not necessarily you need to use Visual Studio code guys whichever IDE tool you like you can go for it there is no hard and fast rule that you need to stick with Visual Studio code or any anything as such whichever you like you can go for it why exactly I'm using visual studio code that is something I can tell you let me open this folder first which the one which we have created this is the folder let's open this okay perfect now why exactly I use I use because of this plugin this extension which I'm using for ansible where it is so basically I'm using ansible plugin for intellisense syntax syntax highlighting and many things I do with that plugin that is what I'm using let me look for it see this this is the one which is installed this is uh basically from Red Hat when civil vs code extension for red hat so this extension ad language support for ansible okay so it does the syntax highlighting it it will help you for indentations and many things so this is the cool ansible plugin which I am using this is again optional if you want to use this you are free to use it if don't okay not required then in that case come back let me write a first Playbook what exactly I'll be doing with this Playbook I can give any name there's no hard and fast tool that you have to stick with some specific naming convention over here I can give Apache dot EML the only thing is make sure that your file is ending with DOT yml or Dot yaml okay both are accepted yaml is a very simple language guys if you want to know about basic syntaxing in yaml then please do let me know in the comment box I'll definitely make a video on yaml from scratch okay now starting with yaml this is how the ml starts with a three dash again this line is completely optional this is if you want to have multiple uh yaml yaml scripts inside the same file then you can use this three dash or this is completely optional okay now starting with how do we start with Playbook firstly start with the name give a name to your playbook so what exactly are we supposed to do so I'll I'll do Apache installation remember in the last session we had some Apache installation done so this is the same thing I'll do it over here Apache installation basically we'll do Apache installation will update the package we'll copy the config file so there are multiple things we will be doing okay so let's let's do that so Apache installation okay this is the name of our play basically now on which where exactly do you want to run so that host needs to be defined that host needs to be defined so this is I want to run it on web server now if you go and look into my localhost guys I got uh if you look into my ansible server I got localhost as well as a group defined now so we got multiple things right now so in such cases if you do all so what will happen this task will get executed on this machine as well as this machine this is something I don't want so what I'll do I'll Define this is only for web servers basically so I'm defined I'm giving the name of my group over here okay now we need privilege now if you want to download any package restart any package copy config so you will need sudo escalated privilege basically so in command line if you use let me show you the command line also over here what is the command we ran and symbol hyphen this is all hyphen M mode before hyphen I was using iPhone b as a become as a root user correct become as a super user hyphen M then we were using this module apt okay so this is what this is your host definition this is your defining your host on which you want to run this is your telling that I want escalated Privileges and this is the module which you're defining okay now that is what I want I'm doing right now over here so I just have become one space and make it as true okay this is you have defined your play related things now this play will have n number of tasks like what all things you want to do make sure you are in line with this where you started this name and host so your task will come basically in the same line so this these are tasks now there are list of tasks which you can Define so the first task is before you go and execute anything like if you want to like if you want to install Apache package on Ubuntu machine that's Ubuntu machine which we have launched what you need to do you need to do a APT update correct you need to update the local packages so what will be the command if I want to do apt updates similar to this command then you will have apt update something like this you will run okay with an argument you can use shell also if you want to use shell module you can even use cell model so this is the first task basically now this is how you will be defining now you can give a name to this task so naming is always important because when your task is getting executed the naming will get displayed on the screen so this naming is very important guys so you need you should always provide a good naming convention so I'm doing updating update OS this is what I'm writing now how do we Define this task like the command does basically I'm not using cell there's a proper task for it module for it so I'll be using this module so the module can be easily defined as this way I'll just give the module name followed by the argument what is the argument apt update that's set this is how from the ad hoc command I am creating a Playbook guide this is how this is that easy it is okay so Playbook writing is not that difficult now let's go back to a second task what is the second task I want to install Apache what was the command we have executed for installing Apache let me write that for you hyphen B hyphen M apt iPhone a unders double quotes name of the package what is the name Apache 2 lipo Apache 2 and then there is a state call present so there are two things now let's go and Define the same thing in a Playbook give a name again a naming convention installing Apache okay two basically now now over here what is the module name apt now apt module has two argument defined one is name of the package the name of the package is Apache 2 second the state correct state is what state is present let's have this present added over here perfect this is how I got one more task added I got apt update task and I got uh a Apache installation task similarly I can have one more task like making sure that the Apache service is started so for doing that I will need service module okay Apache service start okay so Vic okay now in this I will need a service module so they'll let me call the service module over here okay in the service module it will again request two information the name of the service so the name of the service is Apache 2 basically and the state so state is started so started is basically is an analogy used in ansible not just start but it started making sure if the service is not running it will go and start the service for you and if it's already started nothing will happen that is the analogy we use started restarted stopped such analogies we use in up ansible last but not the least I need to copy the configuration files as well so I'll be using the copy module copy config files see how easily I am creating this Playbook so if you know ad hoc commands if you know how exactly these commands are executed I'm just using the module name the name and the state I'm not making it difficult for you guys it's a very simple and simple Playbook which I'm writing later I'll show you how do we add variables okay because if you want to reuse we share this particular playbooks with others you should know how to work with variables okay that is something we'll see now copy command what do I want to copy I want to copy or index.html file so very very exactly the index.html file is I have not created that file yet in the previous session that VM is deleted now guys so I'll have it that index.html so wherever I keep this Playbook I'll create this index.html file over there and then where do where exactly do I want to copy this I want to copy this at where triple w dot HTML slash index.html this is the location okay now this is my playbook is ready I have showed you how from the ad hoc command you can create a Playbook very simple very easy to use uh let me copy this I can use winscp to copy this directly onto my server or just copy paste onto my end server okay let me where I am exactly right now I'm into root let me have something written as Apache Dot yaml and let's copy paste this Playbook perfect now the first thing is we need to go and create a index file to my current directory so let me have this index.html created quickly let's have a syntax HTML welcome to devops Mela okay guys if you're not subscribed to my Channel please do subscribe and please do let me know if there's anything missing if you want me to cover any new topic anything any feedback will be accepted you can put it in the comment box or you can write to me on devopsmela at gmail or you can visit my website devopsmila dot in all the possibilities are available you can reach out to me whichever way you like okay now let's go and execute this Playbook okay okay let's go and run the Playbook and see if it's working okay we got the apache.aml file over here how do we run the Playbook very simple and simple forward by Playbook and the Playbook name apache.yaml let's go and execute the Playbook so the play is running let's see if connecting to this particular server that's my web server updating OS is my first task basically it will go and update the package it will basically run the command apt update let's wait that was successful now going and installing Apache so this is why I have given the naming convention guys the play name is Apache installation task Gathering facts if you want to disable this you can do that there is something I'll talk about later update OS this is the naming convention installing Apache so that's why I've given the naming conventions so you know what what's happening okay again if you want to know more details like while we are waiting for this what's happening in the back end you can use the flag hyphen V while running the sensible playbook for verbose logs wait and this is I'm not running on my Local Host guys till now the ad hoc command which we were executing that was I was executing on my Local Host to just save some time and unnecessarily not spinning up a new VM but this time for executing A playbook I have created a altogether a new host machine and I'm executing this on a different machine right now okay using ansible just to give you a glimpse of in a real world how things happens so this this is basically we are covering the real words real world scenario guys okay let's wait while the installation is taking place turn successful service started perfect so there are total recap you can see a recap over here five okay change three unreachable zero fail zero skipped zero so this is done successfully now how do we verify quickly verify this is the host machine go and open this on a browser because we got Apache installed on top of it welcome to devops Mela perfect okay this is how Now using a Playbook there are n number of tasks which we have executed and now this is how it looks like okay so that's it from Playbook uh in the next video we'll talk about variables how do we Define variables if you want to reuse this task then how do we Define variables that is something we'll talk about in the next video upcoming video thank you very much bye bye hello all welcome to my channel devops mail and again welcome to the ansible web series today we'll talk about ansible variables how do we Define variables inside the Playbook now in the previous session we saw how do we create A playbook this is something we have done in the previous video in which we have created a simple Playbook wherein we have installed Apache onto the end server and then we made some configuration change copied some configuration restarted Apache updated the package this is something we did now why do we need variable now this was the file let me open that file first why do we need variables variables in playbooks are very similar to using variables in any other programming language guys it helps you to assign value that can be used inside the Playbook any given point of time plus variables can make your playbooks reusable you can run this Playbook on N number of systems like you can Define variables like this is the value which is hard coded over your host uh this is the value Apache sorry this is the value the package name Apache 2 is hard coded over here the source and the destination is hard coded over here the service name is hard coded over here so these all values are hard coded you can replace this value easily just imagine if you have a bigger Playbook wherein you have 100 line of 200 line of code written it's very difficult to go and edit each and every line if you want to reuse your playbook so it's better it's always better to use variable define variable in one section and call that call those variable values inside your playbook that is how you can work with variables now how let me show you a small glimpse how do we Define variables let me go back at the top edit this file and have something called variables so let me show you a demo so I'll just use this name so what I'd see more things getting repeated over here is the name of your service your Apache 2 this is getting repeated at multiple location Apache 2. so I just defined Apache 2 over here now I'll go and replace wherever Apache 2 is written I'll just go and replace that how do we call variable that is I'll show you so this is how it is called in ansible under double quotes double bases space call the name of the variable space this is how this is called a variable inside ansible now if you guys are aware of Ginger 2 templating this is ginger to templating format guys so basically this is used used in Python web framework like if you have worked with Django on all we use such formatting over there so the same concept is in the is there in ansible as well so this is a ginger to templating format which we're using same goes over here also if I want to this is how you will be defining variable now this is I've just did for this Apache I'm just giving an example to you same thing wherever you see repeat repetition wherever you see uh some values are getting hard coded like if you see this values this destination is also hard coded and so wherever you see such hard-coded values and if you think we can replace it with variable the value may change in future if you share this particular Playbook with some other developer they may change some values so it's always a good practice to use variables okay now let's go and run this Playbook now the variable is defined let me just make a minor change in my index.html file so that we know there's a configuration change done let's name it as learning variables something like this okay I did a minor change now let me go in run this again okay and symbol hyphen Playbook hyphen the name of the play done it's running again without any issue so this is how you will be defining variables guys variable is always a good practice whenever you're doing anything you're writing any configuration that is what do not repeat yourself so if you're hard coding values it will be very difficult for you to share this Playbook with anyone else or if you want to use the same playbook in future you cannot use it because you need to go inside the Playbook edit the file and while editing you may end up making n number of mistakes okay so it's better to use variable option in ansible okay let me verify if things are looking good the changes which we did is it done because it if you show if you see there was two change in the configuration there was a change and update OS there was a change so wherever you see this yellow this this part was changed where we see this okay file nothing was changed over there welcome to devops Mela learning variable this looks perfect okay so that's it from variable part guys uh this was all about variables now in the next session we got something with ansible roles very important and very uh interesting topic so let's let's come back and let's see what is ansible role thank you very much please do share like And subscribe bye bye hello all welcome to my channel devops Mela this is Rohit Singh okay in my previous video we saw what exactly sensible variables and how do we Define ansible variables today we'll focus on what is ansible roles okay before I talk about what is rules when we start pretty much start with ansible the first thing what we do we start writing playbooks to automate repeating tasks okay has a new new B2 and symbol we like writing playbooks and as a skill gets matured we reach to a point where we where we find ansible Playbook has some limitation okay at that point of time ansible rules comes to your rescue so basically what exactly is rules rules enables you to reuse and share your ansible code more efficiently okay so they will Pro the roles provides a well defined framework and structure for setting your tasks separately for setting variables separately there are different set of handlers which you can Define in rules there are metadata information which you can Define in roles there are different templates Ginger to templates and other files you can use it in roles okay now let's go and see what is rules now basically and symbol this is how ansible rules rules looks like sky roles enable you to reuse and share ansible code efficiently this is something I just spoke about the primary mechanism for rule is for breaking the Playbook into multiple files so if you have a complex Playbook okay so whenever you write a Playbook and if you're doing everything using a Playbook so base your playbook becomes very complex managing Playbook becomes a very difficult and a very tedious task so that is where rules comes in picture so it will simplify writing a complex Playbook and make it easier to reuse okay this is what role will do it for you so like it's a we have created a role of mongodb in this picture what you see and we got defaults files handlers metadata task template test what exactly these are we'll understand that in a bit guys but this is how rule is all about so it will it will give you a framework it will give you a structure it will help you writing a complex playbook in a very easy Manner and those playbooks those rules can be easily used by someone else so you can reuse those rules very easily okay now this is how it looks like ansible if you want to run control like if you want to create your own custom role this is how you'll be using a tool called ansible Galaxy for creating your own role you'll be using a tool called Invincible Galaxy with init and the rule name and this is how you will be creating this structure we'll go and create that structure in a bit but these are some benefits of using roles it will help you organize your playbooks it can help you reshare it can also help you with reusing these are few benefits of ansible roles now let's go and see ansible rules in reality and let's see how do we create ansible role using ansible Galaxy before that let me talk about what are these defaults so these are the default section in this for in this particular folder you can Define the default values of your variable okay in case there there's one more section wherein you can Define the variables in case if you're not defining the variables in this section you can Define it in the default section okay files it will contain if there's any any static or custom files in your role that it will contain those information uh the the task this is basically the task which we are executing if you go back to the Playbook the previous Playbook which we have written all this task will come inside this particular folder now the task folder followed by handlers handlers is it's like you can Define set of handlers that can triggered by task by the rule so what exactly that means like suppose you're making some configuration changes to your web server and you have defined in the Handler that whenever there is a configuration change then it should go and restart my Apache web server only in so such conditions can be defined such triggers can be defined inside the Handler metadata is like if you want to have some metadata information if you need to have some ginger to templates and this is for testing okay this is what the structure of rules look like in reality guys so let me go and create a role and let me show you how exactly it looks like in reality so we'll use the same example of setting up Apache so on my end server uh I don't have Apache installed now so this is something we did on my end server now on my current server like if you see on a local host I don't have Apache installed so we'll use this system itself okay there's no Apache so what we'll do we'll go and set up role we'll create a rule basically so the command is sensible so Galaxy this is not an additional tool when you set up ansible you get ansible ansible Playbook and ansible Galaxy this is the third tool in ansible which we're using right now so it comes by default you don't need any additional installation or anything as such okay we'll do init and I'll basically name it as Apache roll so this is Apache website which I'm setting up right now so I'll stick with I'll give a naming Convention as Apache roll now if you go and see my role is created on my current Drive current location I got this role created let me show you the structure of this role for doing that let me install tree so this will give you a tree like structure you'll know what exactly happening okay okay now let's go inside this folder Apache let's run this tree command now this is you got your default files this is where your default values will be defined this is where all your tasks will be defined this is where your variables will be defined this is where your handlers will be defined okay now let's go and start defining each and every role okay default I'm just skipping default for now because I'll put the values in my variable section so this variable section gets the highest precedent when you're running and executing Your Role so it will go and look into the variable folder and it will look if there is any variable defined if there's nothing defined it will then go and look into the default section okay or if you're defining the variable inside the file even that gives precedence okay so let's go and Define the task the first thing let's go and Define the main.yaml go into the task folder and over here let me open the main.aml file okay now let me go and have all the tasks listed over here it's very simple guys I'll just go and use this task nothing alien I'm doing I'll just copy this entire task which we have written in the Playbook now I'm segregating things let's put it over here that's it we're done the task is defined okay now what else I need to do come back let's go and run the tree again the task is defined now do you want to define the variables okay we didn't Define the variables okay let's go and go back to the task we need to um let's use ginger to templating this variables I'll Define the variable section now not on the top there's no space to define the variable guys okay done perfect okay what else let's go to the variable section now and let's define the variables over there main.dml file now I can Define the variables over here I have only one variable so let's stick with this Apache 2. okay in the variable section I have defined the variables okay perfect now let's come back out and what else we can Define now handlers okay this handlers I have not defined now let's go and Define this handlers how do we Define the handlers let me go you let me just make changes to my handlers file let's go to handlers and open the main.dml5 and what in the Handler what I'll do I'll just move this Apache task restart task inside the Handler so I have given you the condition so this task of restarting Apache this whether it started or not I'll have this part added to my handlers I'll have this added as restart Ed okay if whenever there's a change in the configuration I want this service to be a purchase service let's name it as restarted restart make sense this is the task which I have added in the Handler now how do how how my yaml will know the task will know that there is a Handler which I need to execute which which needs to be triggered so what is the condition Whenever there is a config change I'm just copying this handlet name this is the exact name you'll be using inside your task now okay now whenever there is a config change okay I want the Apache service to be restarted that is what my condition is so how my task will come to know there is a change in config so this is how with so this is I'll come to the config section and over here there is a term called notify so whenever there is a change in this particular task it will notify my Handler and my Handler will run that particular task so this whenever this section is getting executed Whenever there is a change in config my Handler will get notified and this particular whatever task I have mentioned inside my Handler will get triggered automatically so what is what did I mention I've just mentioned to restart my Apache service that's a trigger which which we have mentioned okay perfect looks good and make sure this naming convention whatever name you have given for the Handler is the same name used over you any typo any uppercase lowercase you do the handle will not get executed make that perfect so let's come back now what else I can Define what alls so I have defined handlers I have defined tasks I have defined variables metadata information if you want to put in guys you can put in the metadata information like who has created this and things like that okay who is the author what is the version and things like that if you want to have any metadata information then you can go and do this for now that's it for me now let's go and let me show you how do we utilize this role how do we execute this role now for executing this row you need to write a Playbook I can name it as Apache roll dot yaml so this is a Playbook I'm creating this Playbook will have information such as host it will not have much information it will only have host information and in this host I'll just mention as localhost over here because I got web server also but I'll just stick with localhost for now I'll give privilege escalated privilege so that things can be defined now this is the place where I'll be defining my rules I can have list of rules defined over here but I just want to execute one rule list of rules like for setting a web server you have one role for setting up DB server you have one role for setting up middleware you have one role so this way you can have list of roles defined in my case I just have one role what is the name of my role I forgot Apache roll I believe I'll have to verify that I'll have to verify it is the role Apache underscore role okay Apache underscore role so don't make this typo guys and my role is where it is there in my current directory itself if you see my role is there inside the same directory where my yaml file is created so I got my role over here and I have my ml file over here okay let's go and run this again how do you run it and civil Playbook followed by the roll Apple file perfect this is how you will be executing Your Role let's go and see now I'm executing this on my Local Host now it's getting executed right now now did you see the difference when we write the when we wrote The Playbook it was a quite a big Playbook and we need to it was very loud now when you're writing a complex Playbook just imagine you have a web server middleware server your SQL DB your playbook will be very huge so it's always better and it's always a good practice to use ansible role so that your playbook is organized your variables are organized you can have have handlers and things like that added and then you can red so so you have a very proper format of proper structure of your playbook now this can be easily reused you can share this with someone whatever you want to do so ansible rules comes very handy definitely ansible rules comes later in the stage when you're very comfortable writing playbooks you know how the Playbook works you know how the ansible add-on command works then this rules comes in picture not at the very beginning guys that's why this is added at the very end of my web series so keep that in mind now if you see this the handler was also executed the reason is because there was a change in config now if I just do a curl on my Local Host I can see my web page okay welcome to devops mailer learning variables I can go and check it on the browser also I can find the same thing over there okay now let me let me show you if the Handler is really working or not now I'm not making any changes into the configuration last time when you see last time the handler was executed running Handler now let me just execute this command run this Playbook again and let's see if the Handler is running now because there is no change in configuration now so the eventually the Handler should not run now if you see there's nothing called Handler over here now what I'll do I'll make a changes to my index.file I'll remove this part this learning variables and I'll just go and run the Playbook again now there is a change in my configuration when there is a change in my configuration my Handler should get triggered let's see if it's happening or not perfect you see this there was a change this is how Handler works now let's do a local host welcome to devops mailer so this is all about hair ansible rules guys in the next video I'm thinking of integrating Azure devops with ansible and show you how exactly all this gets integrated and how you can automate this entire process with azure devops let me know in the comment section if you want that part to be covered then I'll go and cover I'll make videos on that and I'll cover that part also in the meanwhile ansible Galaxy there is a separate website there are so many roles already created by others okay so this is the custom rule which I have created but if you go to this community and civil has a very big community and there are so many roles already created which you can just pull and start using it like for example if I go to web and if I look for any web server package like nginx you will you will see n number of rules now if you see this the rule is already there it's created by someone if you want you can just pull this role inside your local system see this is how you'll be using this role and you can create a Playbook and use this role and launch nginx web server so it they might have done some configuration changes to nginx better to go and look what configuration changes they have done whether you can use it in your production or not okay this is done by community people like you and me have created roles and uploaded an ansible Galaxy use it wisely okay make sure it's always recommended if you're doing anything on production create your own custom role okay that's it for me thank you very much see you in the next topic that is integration of ansible with Azure devops thank you very much hello welcome to my channel devops peeler and welcome to the ansible web series this is a continuation video guys in this video tutorial will talk about how do we integrate ansible with Azure devops okay so if you want to automate running your playbooks so Azure devops is the best tool which by which you can integrate and you can run your playbooks okay there are few prerequisite the one prerequisite is setting up Ubuntu VM in that VM you're setting up ansible so this is what we were doing till now we already have Ubuntu vmwarein we have setup ansible we have updated the inventory file and if I go and run the ping command I'm getting a ping from my Local Host basically okay so this looks good just for your understanding in my previous video I have upload I have installed Apache on my localhost that is something I have went ahead and removed it so if you go and check on my localhost if there's any Apache installed so there's no Apache so this is something I have already removed it because I'll be installing it again but this time I'll be installing it using a Playbook okay there are few prerequisite the prerequisites are you need to have ends you should have a count on Azure devops if you're new to Azure devops please go on to dev.ajar.com sign in for free create your organization mailer devops is my organization and this is the project which I have created if you are new guys I do have a complete Azure devop series video please go and have a look on this okay please check my playlist and please check in the description box you'll find the Azure devops series video please go and check how do we work with Azure devops how do we create cicd pipelines with with Azure devops everything is there inside that particular playlist please go and check so one of the playlist one of the prerequisites have a account on Azure devops and have account on Azure cloud once you're done once we are done with all the ansible setup if you are directly watching this video guys please go back and see how did we install and simple how did we set up inventory and all those things in my previous video okay let's come back there are few things more which we need to do firstly we need to set up CLI on this particular machine the the ansible server basically the master node and we need to log in this is for authentication purpose okay now let's go and this is the command for setting up CLI guys I have already ran this command and if you go and look into my you will find the CLI is already installed AC version so I got the CL I already installed now the next command is AZ login basically I'll just run this command AZ login I'll come to the same browser my portal is where where I'm logged in with portal and I'll just put this link followed by the code over here so let's go and put in this code as well once you're successfully logged in you will get an output so let me select my account I got multiple accounts so let me select the account perfect you should get an output over here let's wait okay perfect this is what it is I need this ID so let me copy this ID and keep it on my local for now okay now this command basically if you forgot to copy then you can run this command easy account show this will get you the same information which if you if in any case if you have missed it okay once you're done with this we need to create a service principle and basically this command will get create the service principle for me this is the name of my service principle and I'm giving a role of a contributor and the scope at the subscription level I'm creating the scope is at the subscription level now how what exactly service principle what is service principle how you can create manual and those things everything is covered in my service principle service connection topic please check my Azure devops playlist you will find all this information I have a dedicated video on service principle okay now let me go on copy this command and put it in my so this will basically go and create the service principle for me now let's wait while this service principle is getting created the service principle will be visible in in your Azure ad you can view the service principle over there perfect so these are the information these are some sensitive information which is required so I'll copy this part and I'll put it in my put it over here okay I'll I'll be using this in information that's why I'm copying this now pause that create this directory dot Azure it's a hidden directory which we are creating and create this file credentials so we need this credential file in my VM so let me have this VI forwarded by this and I will need this information and entered in this particular file okay so these are the things I need a subscription ID the subscription ID is right at the top where we have copied where did we copy the subscription ID okay I can get the subscription ID from here also this is a subscription ID I'll need a client ID the client ID is equals to app ID so this is what the app ID so this is all prerequisite guides so this is one time activity which you need to perform when you are integrating your ansible with Azure devops okay secrets and then it will require a tenant ID okay that's it so this is my credit file has got created we have created the credential files okay that's it that is one part is done we have created we have installed Azure CLI on ansible Master server we have setups uh hazy login we have created a service principle now we have created a cred today Azure credential we have stored it in my ansible server so now second part is we need to have a private and public key created basically that key will be required for creating service connection now again if you guys are new to the Azure devops world this service connection and all service principle these jargons May uh annoy you so it's better to go and look into my Azure devops series and if you don't want to watch this complete series there are dedicated videos for service connection and service principle please do watch okay now let's go and get this key created so I'll just copy this command for now this will basically create create a key pair for me uh keep it in the default location do you want to enter passphrase I'll keep it empty for now perfect now I have the private and public key created if you want to see where exactly it is created you can go to this drive it's a hidden file and if you see this you will find the public and private key created okay ID underscore RS ID underscore is dot this is created the next is we need to give 755 permission to this particular folder itself okay next is we need to create a authorize keys in case if you don't have this authorized Keys created create that particular file okay and once created again give a permission of 644 to that particular file okay where I am right now I'm inside the authorized key I'm not okay give this permission okay once this is done unique you can copy this particular ID to your local host okay whatever steps I am doing guys this is I'm setting up ansible server and then I'll be integrating I'll create a service connection and I'll integrate the sensible server with my azure devops okay so let me copy this key copy ID this is a command copy ID all these commands are given in my text in the notepad I'll be sharing that with you guys don't worry so Dave Ops mailer ad I can keep the localhost or I can give the loopback IP perfect this is yes put in the password for the first time okay now this is I'm currently logged in now see the key was added so this was added to my local host now if I want I can connect to my machine using this command also I don't need to have user ID password guys if I want I can connect to my machine now if you see where I have logged in who am I have logged in with devops failure okay now this is how you will be setting up things now I'll go back to my root okay verified everything looks every file verified everything is looking good I have all the prerequisite done setting CLI logging to Azure Cloud creating service principle generating private and public key uh changing the permissions file permission now I have also copied the key to my localhost so that I can log in through SSH so there's no you need of password now I can directly log into SSH to my localhost okay now let's go back and copy ID copy your private key now let me go to this SSH ID RSA so this is basically a private key this is very sensitive information guys you cannot share this information with anyone and you need to keep these Keys very secure in a key vault not on the server so keep that in mind okay so this is the private key what we have now this private key I'll be using this key to logging in onto my for connecting to my Azure devops now I'm inside my Azure devop this is my project Mela devops I'm using this is my organization mailer devops and this is the project which I will be using for this purpose so the first thing is I need to go and create the service connection so go to Project settings come to service connection create new service connection over here look for SSH go next give a host name so basically with before hosting let me paste the private key over here hostname will be your public IP of your ansible server username give a username devops Mela password let's pass in the password okay come down give a name to your service connection I can give anything and simple SSH give a description you can give description Grant all pipelines the permission done so the service connection is created guys this is how we create a service connection the next part is running playbooks using pipelines so I can there are two ways you can run pipelines either classic or yaml both the ways are possible if you want to run through classic even that is possible to run your playbook if you want to run through yaml even that is possible so in this video tutorial we'll cover classic way do let me know in the comment if you if you want to even know how do we integrate with yaml okay please do let me know in the comment box the first thing what is required for running is ansible extension okay so by default the ancible extension is not installed on Azure devops you have to go to the marketplace and click on browse Marketplace and look for n symbol now this is the one which you need to install guys ansible click on this this will take you to get it free and this will take you back to your organization basically and you need to have owner access for doing all those guys you cannot install extension if you if you are just a user on your project so you need to have Euler access if you're not the owner the request will be sent to the owner and the owner will be doing the needful for you now if you see this the extension is already installed on this organization so let's go back to organization so I already have this extension so if I go back to my organization settings extension you can see my ansible extension right over here okay so let's go back and let's have this devsecops again now I'll go to the pipeline before I go to the pipeline I need the playbook in a repository okay so what I can do I can have a new repository created with the name ansible I'll name it as an sensible Playbook to be very specific and we and we have already created A playbook in my previous session guys remember Apache Playbook we have created that is right over here I'll be using the same Playbook let me clone this repository on my local if you are new to git I do have git tutorial video guys please check my playlist okay now coming back this is the folder now this is the file I got the Playbook and I got the index.html file created let me show you that once this is the Playbook basically okay I need to change this the web server I don't have the web server group now I have deleted that VM now so I just have a local host and on the web server we have already installed Apache if you guys are following my complete tutorial series so let me show you the host file once in my host file let's go up this is what is already defined okay so I'll be using this only now if suppose if you want to as I said earlier if you want to do the same thing exact same thing on a different VM make sure there is SSH connection uh established and quickly run the check and symbol all hyphen name ping and see if the ping pong is happening and if everything looks good you can have the IP address replace it with your IP address or the domain name replace it with the username whatever user it is and replace it with your password so exact same thing you can come and add it over here okay nothing different no so this is the prerequisite whatever things we did that was I was setting up ansible server and I was setting up ansible connection with Azure devops now if you want to add any host machine you can simply come and add in the inventory file of your server that's it nothing else okay okay coming back these are the files let me copy and this is my index.html file welcome to devops mailer please like share and subscribe please do it if you like my videos okay coming back [Music] let me copy these files and put it in the git Repository over here perfect let's go get add a quick get ad not a repository let me go inside the repository and civil Playbook let's do a git commit edit Playbook now do a get push so I got everything I have the Playbook I have the index.html file in the same folder now if I come back I should see those details over you okay perfect now I think I should have one folder also created inside this so let me have a folder ansible folder created and put the playbooks inside it this will not required but this is the right way of doing guys okay let's do a git add again git commit again edit folder let's do a get push again perfect refresh it we have it we have the folder inside the folder I got the yaml file and inside that I got the index.html file also keeping it everything in structure manner so this is the right way of doing now I can have I can execute this Playbook from this section also this is my build section I can get this executed using yaml or I can get it get this executed using a classic as well so let's focus on release because mostly we run ansible task at the release side okay so let's come to the release section and use this release section so it's not an hard and fast rule you can run wherever you want I'm just following what exactly is getting followed in the world okay but if you want me if you want me to create a yaml file for executing the Playbook even that is possible I'll show you let's let's let's get this executed I'll show you that even don't worry okay let's go to the release pipeline this is classic I'll show you the yaml method also don't worry both the ways will do it I'll name it as Dev let's keep it manual for now and over here I'll go and add the artifact so I don't have a build pipeline but but I do have my files inside Azure repos so I'll go and select this Azure repose this is a project the sources ansible Playbook the branches main branch and add now I'll just go and look for ansible task and symbol perfect we got the ansible over here so ansible location where is sensible is it your agent machine or remote machine agendas is talking about this agent guys this agent this is a self-hosted agent again if you want to know what is cell phone strategy and what is uh what are this agent pools what are the self-hosted agent what is azure pipelines what is Microsoft hosted agent all this is given in my Azure playlist go and look for Agent video agent job videos or you can go and look for the complete Azure devops end-to-end video okay same like ansible I have created Azure devops videos as well okay please do check now for me my ancible is there on the server it's over here okay so I'll just select the remote machine and all thanks to the SSH endpoint which we have created so I'll select that now where is your playbook now Playbook is there in my remote repository sorry my playbook is there in my repository it's not there on the in civil machine if I want I can keep the Playbook over here and get it executed also directly using pipeline but for now I have it on my repository so I'll just go and select the folder this folder what is the name of your playbook apache.yaml correct that is the name let me verify once this is the name you need to give apache.yaml let me copy and put this okay now where exactly is your inventor inventory file located use default location default is basically it will go into Etc and civil host on the server and it will get you then it will read the inventory file from there or else if you have created a file or else if you want to list the host so you can put the IP address in over here as well or else if you have want to pass in as a Content you can do that also so in my case I'll stick with use default inventory file so that is the default location where I have created my inventory okay I'll name this pipeline as ansible release I'll save this now my ansible task is ready I have the pipeline created now I can go and execute the pipeline as I said it is it will be creating Apache web server and then it will be copying my index.html file to that web server and this is all happening on the Local Host as of now but if you want to do it on some dedicated other VM you can do it make sure put in those VM IP address username and the password in the inventory host file okay keep that in mind that's it that is the only thing you need to do extra nothing else you need to do guys okay now let's go and create a new release and let's execute our first ansible Playbook using Azure devops pipeline okay if there's any error we'll debug that but let's see hopefully I should not get any error but if there's any error I'm going to try and debug that also this may take time guys around couple of minutes so let's pause this video over here and let's come let's come back once the deployment is successful or deployment is filled whatever has happened or else wait let's wait and see what happens you can see the logs in the log section see it is connecting and it started the Playbook run Apache installation Gathering facts perfect Local Host so the connection was good updating always perfect installing Apache now let's wait while the Apache is getting installed okay let me pause this video okay let's wait wait so if you see my playbook was completed it took only 41 seconds if you go inside this it's very similar what we were doing manually from the command line it's very similar config copied changed Apache service started installing Apache updating OS Gathering facts now if you see this total change okay five change three unreachable fail zero so this was successfully deployed how do we verify I can go to my public IP of this localhost and see if we have Apache welcome to devops Mila please like share and subscribe perfect this is how you will be integrating Azure devops with Ensemble to run your playbooks again repeating if you want to add some more host machine just go and update the inventory file that's it nothing much you need to do okay now as I mentioned that this was done using classic if you want to do it using yaml I can do that even for you guys let's create a new pipeline let's have this inside my ansible playbook let's have a starter pipeline created let's remove all this extra things not require let's remove all these steps the only steps is required as ansible step guys so I'll just go and put in ansible okay where is your ansible location this is a remote machine select your SSH where is your playbook it's on agent basically so this agent is right in this folder so I'll have this folder copied this way and I'll have this file name copied this way your inventory file let's use default inventory file location make sure you're at the right place and add this task that's it you got the ansible task added over here save it and run it nothing will happen because it's already installed but this is how you can even execute playbooks using yaml now if you go and see in your repository you will see new yml file got created a yaml file got created over here this yaml file is doing the exact same thing what we did with classic okay so two ways I showed you how do we integrate and civil one is through yaml and one one is sorry two ways I showed you how to run your playbooks using Azure devops pipeline one is through yaml and one is through classic so whichever way you like please go and write by yourself and please do let me know if there's any issue if you're getting stuck you can reach out to me on devopsmail at gmail you can or visit to my website that is dayboxmailer dot in or you can just send me an email I'll try or put it in the comment box I'll try my best to reply as soon as possible thank you very much guys from the sensible series and if you want to look if the pipeline was successful or not see the pipeline is successful nothing much have happened because things are already done using classic but it's the same thing if you go and see the logs it's the same thing okay that's it for me guys thank you very much for your time please put down your thoughts in the comment comment box and if you like dislike if you want me to cover any other topics please do suggest thank you very much thanks for your time bye bye
Info
Channel: DevOps Mela
Views: 6,938
Rating: undefined out of 5
Keywords: Ansible, Tutorial, Playbook, Ad-hoc, Ansible roles, Ansible Integration, Azure DevOps, Linux, Ubuntu, Azure Cloud, Ansible Inventory, Practical, Azure, AWS, Beginner, Training
Id: jDYplrY9p3s
Channel Id: undefined
Length: 117min 31sec (7051 seconds)
Published: Fri Apr 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.