Ansible Playbook Tutorial | Ansible Tutorial For Beginners | DevOps Tools | Ansible Playbook|Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is Rachel from Eddie Rekha and today we'll be learning what is ansible thank you all the attendees for joining today's session so let's get started with it first let us look at the topics that we'll be learning today well it's quite a long list it means you'll be learning a lot of things today let us take a look at them one by one the first we'll see the problems that we're before configuration management and how configuration management helps to solve it we'll see what ansible is and the different features of ansible after that we'll see how nasa is implemented ansible to solve all their problems after that we'll see how we can use ansible for orchestration provisioning configuration management application deployment and security and in the end we'll write some ansible playbooks to install and stack on my node machine and host a website in my node machine so guys I hope that you're all clear with today's agenda so give me a quick confirmation on the chat window so that we can get started okay I've got a yes from Harsha's Ana bhikshus Tao Mahesh and I've got a few more yeses too so we are always ready to get started now before I tell you about the problems let us first understand what configuration management actually is well configuration management is actually the management of your software on top of your hardware what it does is that it maintains the consistency of your product based on its requirements its design and its physical and functional attributes now how does it maintain the consistency it is because the configuration management is applied over the entire lifecycle of your system and hence it provides you with a very good visibility and control when I said visibility it means that you can continuously check and monitor the performances of all your systems so at any time the performance of any of your system is degrading the configuration management system will notify you and hence you can prevent errors before it actually and by control I mean that you have the power to change anything if any of your servers fade you can reconfigure it again to repair it so that it is up and running again or you can even replace the server if needed and also the configuration management system holds the entire historical data of your infrastructure it documents all the snapshots of every version of your infrastructure so over all the configuration management process facilitates the orderly management of your system information and system changes so that it can use it for beneficial purposes ok I've got a question from Harshit he asked that what do you mean by snapshots and where does configuration management actually stored these snapshots well hurshe that's a very brilliant question thank you for asking when I said it documents the snapshot it means that it records the configuration details which are nothing but data descriptions of the overall state of your system at a particular time and where does it store all the snapshots while configuration management system has their own repository which is known as we CMDB or it's called the configuration management database so this is where all the configuration details get stored so we'll be seeing how configuration management tool will actually interact with the configuration management database later in this tutorial so be explaining more about it but I hope that you caught your answer now her ship give me a quick confirmation on the chat window ok say it's got it so hope that the others have understood this as well so let us proceed to the next topic and see the problems before configuration management and how configuration management solved it and with that we'll understand more about configuration management as well so let's see now why do we need configuration management now the necessaries behind configuration management was dependent upon a certain number of factors and certain number of reasons so let us take a look at them one by one so the first problem was managing multiple servers now earlier every system was managed by hand and by that I mean that you have to login to them via SD such make changes and then log off again now imagine if the system administrator would have to make changes and most of the number of servers you'll have to do this task of logging in making changes and long enough again and again repeatedly so this would take up a lot of time and there is no time left for the system administrators to monitor the performances of the system continuously safe at any time any of the servers would fail it took a lot of time people detect the faulty server and took even more time to repair it because the configuration scripts that they wrote was very complex and it was very hard to make changes on to them after configuration management system came into the picture what it did is that it divided all the systems in my infrastructure according to their dedicated task their design or architecture and the organized mess system in an efficient way like I've grouped my web servers together my database servers together application servers together and this process is known as baselining now let's for an example say that I wanted to install lamb stack in my system and lamp stack is a software bundle where L stands for Linux a for Apache M for MySQL and P for PHP so I need this different software's for different purposes like I need Apache server to host my web pages I need PHP for my web development I need Linux as my operating system and MySQL as my data definition language or data manipulation language since now all the systems in my infrastructure is baseline I would know exactly where to install each of the software's for example I'll use Apache as my web server here for database I will install the MySQL here and also began easy for me to monitor my entire system for example if my web pages are not running I would know that there is something wrong with my web servers so I'll go check in here I don't have to check the database servers and application servers for that similarly if I'm not able to insert data or extract data from my database I will know that something is wrong with my database servers I don't need to check these two for that matter so what configuration management system did with baselining is that an organized mess system in an efficient way so that I can manage and monitor all my servers efficiently so guys I hope you've understood this if you have any questions you can always pop it in the chat window and anytime now let us see the second problem that we had which was scaling up and scaling down see nowadays you can come up with requirements at any time and you might have to scale up or scale down your systems on the fly and this is something that you cannot always plan ahead and scaling up your infrastructure doesn't always mean that you just buy new hardware and displace them anywhere happily you cannot do that you also need to provision and configure these new machines properly so with configuration management system I've already got my infrastructure baseline final exactly how these new machines are going to work according to their dedicated task and where should I actually place them and the scripts that configuration management uses are reusable so you can use the same scripts that you use to configure your older machines to configure your new machines as well let me explain it to you with an example let me explain it to you with an example let's say that if you're working in an e-commerce website and they decide to hold a mega sale a new your Christmas sale or anything so it's obvious that there is going to be a huge rise in the traffic so you might need more web servers to handle that amount of requests and you might even need a load balancers or maybe to to distribute that amount of traffic onto your web servers and these changes however need to be made at a very short span of time so after you've got the necessary hardware you also need to provision them accordingly and with configuration management you can easily provision these new machines using either recipes or play books or any kind of script that configuration management uses and also after the sale is over you don't need that many web servers or load balancer so you can disable them using the same easy scripts as well and also scaling down it's very important when you're you in cloud services when you do not need any of those machines it's no point in keeping them so you have to scale down as well because you have to reconfigure your entire infrastructure as well and with configuration management it is a very easy thing to auto scale up and scale down your infrastructure so I think you all have understood this problem and how configuration management solves it so let us take a look at the third problem the third problem was the work velocity of the developers were affected because the system administrators were taking time to configure the server's after the developers have written a code the next job is to deploy them on different servers like test servers and production servers for testing it out and releasing it but then again every server was managed by hand before so the system administrators again have to do the same thing log into each server configure them properly by making changes and do the same thing again to all servers so this was taking a lot of time now before DevOps came is a picture there was already agility in the developer's end for which they were able to release new softwares very frequently but it was taking a lot of time for the system administrators to configure the servers for testing the developers would have to wait for all the test results and this highly hampered the work velocity of the developers but after there was configuration management the system administrator had got access to a configuration management tool which allowed them to configure all the servers at one go all they had to do is write down all the configurations and write down the list of all the software's that they need to provision the servers and deploy it on all of the servers at one go so now agility even came in to the system administrators and as well so now after configuration management the developers and the system administrators were finally able to work in the same pace now this is how configuration management solves the third problem now let us take a look at the last problem now the last problem was rolling back and today's scenario everyone wants to change and you need to keep making changes frequently because customers will start losing interest if things stay the same so you need to keep releasing new features to upgrade your application even giants like Amazon and Facebook they do it now and then and still they are unsure if the users are going to like it or not now imagine if the users did not like it they would have to roll back to the previous version again let's see how it creates a problem now before there was configuration management let's say you've got the old version which is the version 1 when you're upgrading it you're changing all the configurations in the production server you're deleting the old configurations completely and deploying the new version now if the users did not like it you would have to reconfigure this server again with the old configurations and that will take up a lot of time your application is going to be down for that amount of time that you need for reconfiguring the server and this might create a problem but when you're using configuration management system as we know that it documents every version of your infrastructure when you're upgrading it with configuration management it will remove the configurations of the older version but it will be well-documented it will be kept there and then the newer version is deployed now if the users did not like it this time the older of configuration version was already documented so all you have to do is just switch back to the old version and this won't take up any time and you can upgrade or rollback your application in zero downtime zero downtime means that your application would be down for zero time it means that the users will not notice that your application went down and you can achieve it seamlessly and this is how configuration management system solves all the problems that works before so guys I hope that if all understood how configuration management did that if you have any questions you can pop it in the chat window let us now move on to the next topic now the question is how do I incorporate configuration management in my system well you do that using configuration management tools let us take a look at all the available configuration management tools so here I've got the four most popular tools that is available in the market right now I've got ansible and foul stack which are push based configuration management tool by push-based I mean that you can directly push all those configurations on to your node machines directly while chef and puppet are both full based configuration management tools it means that they rely on a central server for configurations they pull all the configuration from a central server there are other configuration management tools available in the market too but but before are the most popular ones so now let's know more about ansible now ansible is a configuration management tool that can be used for provisioning orchestration application deployment automation and it's a push based configuration management tool like I told you what it does is that it automates your entire IT infrastructure and gives you large productivity gains and it can automate pretty much anything it can automate your cloud your network your servers and all your IT processes ok I've got a question from Dikshit up what does automation exactly mean it's a very good question Vic sure thank you so much well you can think of automation as a solution for turning your infrastructure administration into a code base and it described all the processes necessary for deploying a server in a set of provisioning scripts that can be reused again well an easy word stick shift our automation is just writing a script about how and what you want all your systems to do and don't worry we've been learning how to write the scripts and ansible they're called answer the playbooks and we'll be learning that later in this tutorial so this r is clear with what automation is give me a quick confirmation on the chat window so that I can proceed ok she says yes so let us move on to the next topic so now let us see the features of ansible the first feature is that it's very simple it's simple to install and setup and it's very easy to learn because ansible playbooks are written in a very simple data serialization language which is known as Gamal and it's pretty much like English so anyone can understand that and it's very easy to learn next feature because of which ansible is preferred over other configuration management tools is because it's agentless it means that you do not need any kind of agents or any kind of client softwares to manage your node machines all you have to do is install an sigil in your control machine and just make an SSH connection with your nodes and start pushing configurations right away the next feature is that it's very powerful even though you call ansible simple and it does not require any agent it has the capabilities to model very complex IT workflows and it comes with a very interesting feature which is called the batteries included it means that you've got everything that you already need and in ansible it's because it comes with more than 750 inbuilt modules which you can use them for any purpose in your project and it's very efficient because all the modules that ansible comes with they are extensible it means that you can customize them according to your needs and for doing that it do not need to use the same programming language that it was originally written in you can choose any kind of programming language that you're comfortable with and then customize those modules for your own use this is the power and liberty that ansible gives you so hope guys that is all understood the features these are pretty simple so moving on to the next topic and if you have any questions you may ask me anytime on the chat window now let us take a look at the case study of NASA what were the problems that NASA was facing and how ansible solved all those problems now NASA is an organization that has been sending men to the moon they are carrying out missions and Mars and they're launching satellites now and then to monitor the earth and not just the earth they are even monitoring other galaxies and other planets as well so you can imagine the kind and the amount of data that NASA might be dealing with but all the applications were in a traditional hardware based data center and they wanted to move into a cloud-based environment because they wanted better agility and they wanted better adaptive planning for that and also they wanted to save costs because a lot of money was spent on just the maintenance of the hardware and also they wanted more security because NASA is a government organization of the United States of America and obviously they wanted more security because NASA is a government organization of the United States of America and hold a lot of confidential details as well for the government so they just cannot always rely on the hardware to store all these confidential files they needed more security because if at any time the hardware fails they cannot afford to lose that data and that is why they wanted to move all their 65 applications from a hardware environment to a cloud-based environment now let us take a look what was the problem now for this migration of all the data into a cloud environment they contacted a company called in frozen now in frozen is a company who is a cloud broker and integrator who implement solutions to meet needs with security so incident was responsible for making this transition and now if I wanted to make this transition in a very short span of time so all the applications were migrated as it is into the cloud environment and because of this all the AWS accounts and all the virtual private clouds that was previously defined they all got accumulated in a single data space and this made of a huge chunk of data and NASA had no way of centrally managing it and even simple tasks like giving a particular system administrator access rights to a particular account this became a very tedious job well NASA wanted to automate end to end deployment of all their apps and for that they needed a management system so this was the situation when NASA moved into the cloud so you can see that all those AWS accounts and virtual private clouds they've got accumulated and made a huge chunk of data and everyone was accepting directly to it so there is a problem in managing the credentials for all the users and the different teams but NASA needed was divide up all their inventories all their into group and number of holes and also they wanted to divide up all the users into different teams and give each team different credentials and permissions and also if you look in the more granular level each user in each team could also have different credentials and permissions let's say that you want to give the team leader of a particular team access to some kind of data but I don't want the other users in the team to access that data also NASA wanted to define different credentials for each individual member as well they wanted to divide up all the data according to the projects and jobs also now I wanted to move from chaos into a more organized manner and for that they adopted ansible Tower now ansible tower is ansible in more enterprise level ansible tower provides you with the dashboard which provides all the state of summary of all the hosts and jobs now anthem of tower is a web-based interface for managing your organization it provides you with they're very easy to use user interface for managing quick deployments and monitoring all the configurations let's see what ansible tower did it has a credential management system which could give different access permission to each individual user and teams and also divided of the user into teams and single individual users as well and it has a job assignment system and you can also assign jobs using ansible tower it's supposed let's say that you have assigned job one to a single user job to another single user while job c would be assigned to a particular team similarly the whole invention was also managed all the servers let's say dedicated to a particular mission was grouped together all the host machines and other systems as well sensible tower helped NASA to organize everything now let us take a look at the dashboard that ansible tower provides us so this is the screenshot of the dashboard at a very initial level you can see right now there are 0 hosts nothing is there but I'm just showing you what ansible tower provides you so on the top you can check all the users and teams you can manage the credentials from here you can check your different project and inventories you can make job templates and shed you with jobs as well so this is where you can schedule jobs and provide every job with a particular ID so that you can track it you can take your job status here whether your job was successful or failed and since ansible tower is a configuration management system it will hold the historical data as well so you can take the job statuses of the past month or the month before that you can check the whole status as well you can check how many hosts are up and running you can see the host count here so this dashboard of ansible tower provides you with so much ease of monitoring all your systems so it's very easy to use ansible tower dashboard anyone in your company anyone can use it because it's very user friendly now let us see the results that NASA achieved after it has used ansible Tower now updating nasa.gov used to take one hour of time and after using ansible it got down to just five minutes security patching updates we're a multi-day process and now it requires only 45 minutes the provisioning of OS accounts can be done in just 10 minutes earlier the application stack up time required one to two hours and now it's done in only 10 minutes it also achieved near real-time ram and disk monitoring and baselining all the standard Amazon machine images this used to be a one hour manual process and now you don't even need manual interference for that it became a background invisible process so you can see that how ansible has drastically changed the overall management system of nasa so guys I hope that if understood how ansible helped NASA if you have any question you may ask me at any time on the chat window let us proceed to the next topic now this is all about how others have use ansible so now let us take a look at the ansible architecture so that we can understand more about ansible and decide how we can use ansible so this is the overall ansible architecture I've got the ansible automation engine and I've got the inventory and a playbook inside the automation engine I've got the configuration management database here and host so Harshit that you're asking me about that where does it store all the configuration details this is stored in this configuration management database which could be in your cloud or it isn't could be in your bare metal server and this configuration management database is a repository that acts as the data warehouse for all your IT installations it holds all the data relating to the collection of your all IT assets and these are commonly known as configuration items and it also holds the data which describe the relationships between such assets so this is a repository for all your configuration management data and here I've got the ansible automation engine I've got the inventory and inventory is nothing but the list of all the IP addresses of all my host machines now as I told you how to use configuration management you use it with the configuration management tool like ansible but how do you use ansible well you do that using playbook and playbook describe the entire workflow of your system inside play books have got modules api's and plugins now modules are the core files now play books contain a set of place which are a set of tasks and inside every task there is a particular module so when you run a playbook it's the modules that actually get executed on all your node machines the models are the core files and like I told you before ansible already comes with inbuilt modules which you can use and you can also customize them as well it comes with a different cloud module database modules and don't worry I'll be showing you how to use those modules in ansible and there are different API is as well well API is and ansible are not meant for direct consumption they're just there to support the command line tools for example they have the Python API and these API is can also be used as a transport for cloud services whether it's public or private you can use it then ascott plugins now plugins are a special kind of module that allow to execute ansible tasks as a job build step and plugin for pieces of code that argument the ansible core functionality and ansible also comes with a number of handy plugins that you can use for example you have action plugins cash plugins callback plugins and also you can create plugins of your own as well let me tell you how exactly different it is from a module let me give you the example of action plug-in now I can plug in our front-end modules and what it does is that when you start running a playbook something needs to be done on the control machine as well so this action plugins trigger those action and execute those tasks in the controller machine before calling the actual modules that are getting executed in the playbook and also you have a special kind of plugin called the connection plug-in which allows you to connect to the docker containers in your node machine and many more and finally have this host machine that is connected via SSH and these host machines could be either Windows or Linux or any kind of machines and also let me tell you that it's not always needed to use as I said for connection you can use any kind of network authentication protocol you can use Kerberos and also you can use the connection plugins as well so this is fairly a very simple ansible architecture so guys I hope you have all understood give me a quick confirmation on the chat window it says yes big out her okay so now that you've understood the architecture let us write a playbook now now let me tell you how to write a play book and playbooks and ansible are simple files written in Yambol code and yeah mo is a data serialization language you can think of data serialization language as a translator for breaking down all your data structure and serialize them in a particular order which can be reconstructed again for later use and you can use this reconstructed data structure in the same environment or even in a different environment so this is a control machine where ansible will be installed and this is where you'll be writing your playbooks let me show you the structure of how to write a playbook now every playbook starts with three dashes on the top the first thing have to mention the list of all your host machines here it means where do you want this playbook to run then you can mention very by gathering facts then you can mention the different tasks that you want now remember that the past get executed in the same order that you write them for example you want to install software a first and then softer beef later on to make sure that the first task would be install software and the next house could be install software B and then I've got handlers at the bottom the handles are also tasks but the difference is in order to execute handlers you need some sort of triggers in the list of tasks for example we use notify I'll show you an example now ok let me show you an example of playbook so that you can relate to this structure so this is an example of an ansible playbook to install Apache like a tolling starts with three dashes on the top remember that every list starts with a dash in the front or a hyphen here are only mentioned just the name of one group you can mention the name of several groups where you want to run your PlayBook then I've got the tasks you give a name for the task which is install Apache and then you use a module Here I am using the apt module to download the package so this is the syntax of writing the apt module to give the name of the package which is Apache to update cache is equal to yes it means that it'll make sure that apt-get is already updated in your node machine before it installs the Apache - and in mentioned state equal to latest it means that it will download the latest version of Apache - and this is the trigger because I'm using Handler is here right and the handler here is to restart Apache and I'm using the service module here and the name of the software that I want to restart is Apache - and state is equal to restart it then notifies mentioned that there is going to be a handler whose job would be to restart Apache - and then the tasks in the handler would get executed and we to restart Apache - so this is a simple playbook and we'll also be writing similar kind of play books later on the hands-on part so you'll be learning again so if it's looking a little gibberish for you will be doing on that on the hands-on part so then it will clear all your doubts so now let us see how to use ansible and understand its applications so we can use ansible for application deployment configuration management security and compliance provisioning and orchestrations let us take a look at them one by one first let us see how we can use ansible for orchestration ok I've got a question from Mahesh what is orchestration well orchestration means well Mahesh let's say that we have defined configurations for each of my systems but I also need to make sure how this configurations will interact with each other so this is the process of orchestration where I decide that how the different configurations on different of my systems and my infrastructure would interact with each other in order to maintain a seamless flow of my application and your application deployments need to be orchestrated because you've got a front-end and back-end services you've got databases you've got monitoring networks and storage and each of them has their own role to play with with their configuration and deployment and you cannot just run all of them as ones and expect that the right thing happens so what you need is that you need an orchestration tool that all these tasks happen in the proper order that the database is up before the backend server and the front-end server is removed from the load balancer before it gets upgraded and that your networks would have their proper VLANs configured so this is what ansible helps you to do so make sure to understand what orchestration exactly is ok he says yes ok so let me give you a simple example so that you can understand it better let's say that I want to host a website on my node machines and this is precisely what we're going to do later on the hands-on part so first and in order to do that first I have to install the necessary software which is the lamp stack and after that I have to deploy all the HTML and PHP files on the web server and after that I'll be gathering some kind of information from my web pages that will go inside my database server now if you want to perform these out you have to make sure that the necessary software that installs the first I cannot deploy the HTM and PSD files on the web servers if I don't have the web servers if a party is not installed so this is orchestration where you mentioned that the task that needs to be carried out before and the task that needs to be carried out later so this is what ansible Play Books allow you to do so Mahesh and everyone else I hope that you've understood what orchestration is to give me a quick confirmation on the chat window so that we can move to the second application okay I've got a few yeses now let's see what provisioning is like provisioning in English means to provide with something that is needed it is same in case of ansible it means that ansible will make sure that all the necessary softwares that they need for your application to run is properly installed in each of the environment of your infrastructure let us take a look at this example here to understand what provisioning actually is nice I want to provision a Python web application that I'm hosting on Microsoft Azure and Microsoft Azure is very similar to AWS and it is also a gout platform on which you can build up all your applications so let's say so nice I want to host my sis I'm developing a Python web application for coding I would need the Microsoft if your document database I would mean Visual Studio we need to install Python also and some kind of software development kit and different API for that so ansible you can list out the name of all the software development kits and all this necessary software's that you will require for coding this web that it would require in order to develop your web application so you can list out all the necessary software's that you'd be needing in an ansible playbook in order to develop your web application and for testing your code out you will again need Microsoft reviewer document database you would again this visual studio and some kind of testing software again you can list out all the software's in ansible playbook and it would provision your testing environment as well and it's the same thing while you're deploying it on the production server as well ansible will provision your entire application at all stages at coding stage testing and at the production stage also so guys I hope you've understood what provisioning is let us move on to the next topic and see how we can attain configuration management with ansible now ansible configurations are simple data descriptions of your infrastructure which is both human readable and machine possible and ansible requires nothing more than an SSH key in order to start managing systems and if you start managing them without installing any kind of agent or client software so you can avoid the problem of managing the management which is very common in different automation systems for example I've got my host machine and our party web server is installing each of the host machine I've also a PHP and MySQL installed if I want to make configuration changes if I want to update a party and update my MySQL I can do it directly I can push those new configuration details directly onto my host machines or my node machines and my server and you can do it very easily using ansible playbooks so guys are clear about what is configuration management with ansible if make with confirmation on the chat window ok I've got a few yeses from Mahesh and hard are ok so let us move on to the next topic let us see how application deployment has been made easier with ansible now ansible is the simplest way to deploy your applications it gives you the power to deploy all your multi-tier applications where reliably and consistently and you can do it all from a common framework you can configure all the needed services as well as push application artifacts through one system with ansible you can write tables which are the description of the desired state of your system and it is usually kept in the source control sensible then does all the hard work for you to get your systems to the state no matter what state they're currently in and playbooks make all your installations on your upgrades for day-to-day management very repeatable so with ansible you can write pay books which are the descriptions of the desired state of the systems and these are easily kept in the source control and symbols and does all the hard work for you to get all your systems in the desired state no matter what state they're currently in and pay books make all your installations your upgrades and for all your day-to-day management in a very repeatable and reliable way so let's say that I am using a version control system like git while I'm developing my app and also I'm using Jenkins for continuous integration now Jenkins will extract gold from get every time there is a new commit and then major software build and later this build will get deployed in the test server for testing now as changes are kept making in the codebase continuously you will have to configure your tests and the production server continuously as well according to the changes so what ansible does is that it continuously keeps on checking the version control system here so that it can configure the tests and the production server accordingly and quickly and hence it makes your application deployment like a piece of cake so guys I think you have understood the application deployment don't worry and the hands-on part will also be deploying our own applications on different servers as well so guys are we ready to proceed to the next topic give me a quick confirmation ok I've got a few yeses now let us see how we can achieve security with ansible in today's complex idea environment security is paramount you need security for your systems you need security for your data and not just your data your customers data as well not only you must be able to define what it means for your systems to be secure you also need to be able to simply apply that security and also you need to constantly monitor your systems in order to ensure that they remain compliant with that security and with ansible you can simply define security for your systems using playbooks with playbooks you can set up firewall rules you can log down different users or groups and you can even apply custom security policies as well now ansible also works with the main point group which writes ansible rules to apply d Sestak now d Cystic is a fiber security methodology for energizing security protocols within your network servers and different computers and also it is very compliant with the existing SSS and winner in protocols and this is also a reason why ansible is preferred over other configuration management tools and it is also compatible with different security verification tools like opens gap and stigma what tools like OpenScape and stigma does is that it carries out a timely inspection of all your software inventory and check for any kind of vulnerabilities and it allows you to take steps to prevent those attacks before they actually happen and you can apply the security over your entire infrastructure using ansible so guys I hope that if understood security with ansible if you have any questions you may ask me anytime on the chat window so how about some hands-on with ansible so let us write some ansible play books now so what are we going to do is that we are going to install Landsat and then we're going to host a website on the Apache server and will also collect some data from our webpage and store it in the MySQL server so guys let's get started so here I'm using the Oracle VirtualBox manager and here I've created two virtual machines the first is the ansible control machine and the ansible host machine so Antigo control machine is the machine where I have installed ansible and this is where I'll be writing on my playbooks an ansible host one here is going to be my node machine this is where the playbooks are going to get deployed so in this machine I'll deploy my website so I'll be hosting a website in the ansible host one so let us go to my control machine and start writing the playbook so this is my ansible control machine now let's go to the terminal first so this is the terminal of my ansible control machine and now I've already installed ansible here and I've already made an ssh connection with my node machine so if you want to know how to install answer well on how to make an ssh connection with your nodes you can refer to my previous video we have shown you how to set up an Sibyl and make it such connection with your notes so let me here just become the routines office now you should know that you do not always need to become the root user in order to use ansible I'm just becoming the root user for my convenience because I like to get all the root privileges while I'm using answers but you can't order to any user if you like so let me clear my screen first now before we start writing playbook set of first check the version of ansible that is installed here and for that I'll just use the command ansible - - version and as you can see here that I have got the ansible two point two point zero point video version here now let me show you my hosts inventory files since I've got only one load machine here I'm going to show you where exactly the IP address of my node machine is being stored so open the host file for you now so I'm just going to open the file and show it to you I'm using the GI disk editor and the default location of your hosts inventory file is at seek ansible slash and this is your hosts inventory file and now have mentioned the IP address of my host machine here which is 192 point one 58.50 6.1 video - and i've named it under the group name - service so i'll just write the name of your group under the square bracket now I just have one node machine so there is only one IP address if you have many node machines you can just let down the IP address under this line it's as simple as that or if you even want to group it under a different name you can use a different name use another square bracket and put a different name for another set of your hosts okay now let me clear my screen first so first let me just pass out the SSH connection whether it's working properly or not using ansible so for that I'll just type in the command and Sybil and think and then the name of the group of my host machines which is death servers in my case and paintings to call it means that an SSH connection is already established between my control machine and my node machine so we are all ready to write date books and start blowing it on the nose so the first thing that I need to do is write a provisioning table now since I'm going to host a website I would first need to install the necessary software's so I'll be writing a provisioning playbook for that and I was provisioned my node machine using lamp stack so let us write a playbook to install lamp stack on my node machine now it's already written that playbooks I'm just going to show it to you I'm using the T edit editor again and the name of my provisioning table is lamp stack and the extension for a Yama file is dot yml and this is my playbook now let me tell you how I have written this playbook as I told you that every table starts with 3 dashes on the top so here are the 3 dashes and then I've given a name to this label which is to install Apache PHP and MySQL now I've already got the L in my lamp because I'm using a boon to machine which is a Linux operating system so I need to install our party PHP and - given now and then you have to mention the host here on which he won this kay book to get deployed so I've mentioned test server here and then I want to escalate my privileges for which I'm using become and become user it is because sometimes you want to become another user different from what you are actually logged into the remote machine so you can use escalating privileges tools like foo or sudo to gain root privileges and so and that is fires used to become and become usual for that so I'm becoming the user group and I'm using become true here on the top what it does is that it activates your privilege escalation and then you become the group user on the remote machine and then gathering facts true now what it will do is that we gather useful variables about the remote host now what exactly did we gather is some sort of files or some kind of teeth we can be use later in a different play book and as you know that sv playbook is a list of tasks that you need to perform so this is a list of all my tasks that I'm going to perform and since it's a provisioning playbook which means I'm only installing the necessary software that will be needed in order to host a website on my note machine so first I'm installing a party so I've given the task name as install apache2 and then I'm using the package model here and this is the syntax of the package module so you have to first specify the name of the package that you are going to download which is Apache 2 and then input state equal to present now since you're installing something for the first time and it won this package to be present in your node machine so you're putting state equal to present now similarly if you want to believe something you can put state equal to absent and it works that way so I've installed in Apache PHP module and I've installed PHP client the HTM scripts PHP DD library I've installed a package PHP MySQL and finally has installed the MySQL server in the similar way that has installed a party too and this is a very simple playbook to provision your node machine and actually all the playbook for simple I hope that you have understood how to write a playbook now let me tell you something that you should always keep in mind while you were writing play books make sure that you are always extra careful with the indentation because Yambol is a data serialization language and it differentiates between elements with different indentations for example I've got a name here and a name here also but you can see that the indentations are different it is because this is the name of my entire playbook while this is just the name of my particular task so because 2 are different things and they need to have different indentations the one should the similar invitations are known as siblings like this one this is also doing the same thing this will also install in some kind of package and this is also installing some kind of package so these are similar so that's why you should be very careful with indentation otherwise it will create a problem for you so now I hope that if understood how I have written this playbook if you have any doubts you can ask me questions right away so can I get a quick confirmation that you all understood this ok ok everyone has said yes so what are we waiting for let us run this playbook clear my screen first so in order to run a table and the command that you should be using to run an answer book playbook is ansible - playbook and then the name of your file which is glam stack yeah me and here we go and here it is okay because it is able to connect to my nose machine a party - has been installed and it's done my playbook is successfully run and how do I know that I know that scene beef common return values so these common return values like okay I changed unreachable and face they give me the state of summary of how much table restaurant so ok equal to eight it means there are eight tasks that was run okay changed equal to seven it means that something in my note between has been changed because obviously I've installed new packages into my node machine through its showing change equal to seven unreachable is equal to zero it means that there is evil hosts that were unreachable and Saint equal to zero it means that geo tasks will fade so my playbook was run successfully on to my node machine let us check my note messy Nancy a party n my skill has been installed so let us go to my node machine now so this is my node machine let us check now for our tea server has been installed so I'm going to know web browser so this is my web browser in my node machine let me go to the localhost and check if a fashion web server has been downloaded and it's there it works now this is the default web page of Apache - web server so now I know for sure that Apache was installed in my node machine now let us see if my SQL server has been installed let me go to my terminal this is the terminal of my node machine now if you want to check if MySQL installed just use this forming two month MySQL user root and then - see sudo password a smidgen for MySQL and there it is so MySQL server was also successfully installed in my node machine let's go back to my control machine and let's do what is left to do so we are back into our control machine now i've already provisioned my node machine further see what we need to do next now since we are deploying a website on the node machine let me first showing you how my first web page looks like let me first show you how my first web page looks like so this is going to be my first web page which is index dot HTML and I've got two more PHP files also this shall be actually deploying these files onto my node machine so let me just open the first web page to you so this is going to be my first web page and what I'm going to do is that I'm going to ask for name and email because this is a registration page for Eddie Rachel where you have to register with your name and email and I want this name and email to go into my database so for that I need to create a database and also need to create a table for this name and email data to store into so for that will write another playbook and we'll be using database modules in that clear the screen first now again I've already written that playbook so let me just show it to you I'm using the T edit editor here again and the name of this playbook is my external module okay so this is my playbook so like all table it starts with three dashes and here I've mentioned host all now I just have only one host I know I could have mentioned either the only one I see address directly or even given the name of my group but I didn't just all here so that you can know that if you have many group names or to have many nodes anyone this playbook to run on all of your node machines you can use this all and this table will get deployed on all your node machines so this is another way of mentioning your host and I'm using remote user root and this is another method to escalate your privileges it's similar to become and become user so on the remote user to have root privileges while this cable could run and then the list of the tasks so what I'm doing in this case book is that since I have to connect to my MySQL server which is present in mine old machine I need a particular software for that which is the MySQL Python module and I'm going to download and install it using pip now pip is the Python package manager with which you can install and download Python packages but first I need to install fit in my node machine so since I told you that the tasks that you write in a playbook it gets executed in the same order that you write them so my first task is to install pip and then I'm using the apt module here here I've given the name of the package which is fighting fit and state equal to present and after that I'm installing some other software's using this and I'm stalling some other related software as well I'm also installing library - click line death and after that using pip I'm installing the MySQL Python module now notice that so you can consider this as an orchestration playbook because here I'm making sure that it has to get installed first and after pip is installed I'm using pips to install another Python package so you see what we did here right and then I'm going to use the database modules for creating a new user to access the database and then I'm creating the database named Eddie so for creating a MySQL user I have used the MySQL user database module that ansible comes with and this is the syntax of the - quill user module it clear the name of the new user which is Eddie wake up you mentioned the password and the prettier it means what privileges do you want to give it to the new user and here I'm granting all privileges for all database and since you're creating it for the first time and you want state to be present Cimmerian using the MySQL DD module to create a database in my MySQL server named Eddie so this is the very simple syntax of using my SQL DB modules we have to just give the name of the database in DB equal to and state equal to present so this will create a database named Eddie and also and after that asked me to created people inside the database for storing my name and image details right and and unfortunately ansible does not have any MySQL tables creating modules so what I did is that I've used the command module here with command modules I'm directly going to use my SQL queries to create the table and the syntax is something like this you can write it down or remember it if you want to use it so for that since I'm writing a MySQL query I started with MySQL user Eddie wake up the - U is for the user and then for password it's ready wake up now after - II just write the query that you need to execute on the MySQL server and write it in single quotation so I have written the query to create a table and this is create table ret the name the email and then after that just mention the name of the database on which you want to create this table which is Eddie for me so this is my orchestration playbook I hope that is understood have written this if you have questions you may ask me anytime so guys should we run this playbook so harsh it says go so let's go clear my screen first now do you remember the command to run the playbook yes you're absolutely right big Sheeta the command is answered with a - play hook and the name of your playbook which is MySQL model and here we go again my common return values tell me that label person successfully because there are no failed tasks and no unreachable hope and there are change stars in my node machine so now all the packages are downloaded now my node machine is well provision it's properly orchestrated now what are we waiting for let's deploy our application well clear the screens first so now let me tell you what exactly do we need to do in order to deploy my application and in my case these are just three PHP files and HTML files that I need to deploy it on my node machine in order to display this HTML files and PhD files on my web server in my node machine what I need to do is that I need to copy this files from my control machine to the proper location in my node machine and we can do that using playbooks so let me just show you the playbook to copy files and the name of my file is deploy website so this is my playbook to deploy my application and here again I've used three dashes and then the name of my playbook is copy the host as you know that it's going to be dead service I'm using privilege escalation again and I'm using become and become user again and gather facts again true and here is the list of the task and the task is to just copy my file from my control machine and paste it in my destination machine which is my node machine and for that and for copying has used the copy module and copy module is a file modules that ansible comes with so this is the syntax of the copy module here you just need to mention a source and source is the path where a fire is contained in my control machine which is homemade eureka documents and the name of the pilots index.html and i wanted to go too far ww HTML and it's index or HTML so I should be copying my files into this location in order for it to displayed on the web page and similarly I've copied my other PHP files using the same copy module as mentioned the source and destination and copying them to the same destination from the same source final thing any of you would have questions there this is the most easiest playbook that we have written today so let us apply our application now and for that we need to run this table and before that we need to clear the screen because there are a lot of stuff on our screen right now so let's run the playbook and here we go and it was very quick because there was nothing much to do you just have to copy files from one location to another and these are very small files let us go back to a host machine and see if it's working we're back again at our host machine let's go to my web browser to check that so let me refresh it and there it is and so here is my first web page so my application was successfully deployed so now let us enter our name and email here and check if it is getting entered in my database so let's put our name and the email tips for you see dot-com and add it the new record created successfully means that it is getting inserted into my database now let's go back and view it and there it is so congratulations you have successfully written playbooks to deploy your application you have provisioned your node machines using playbooks and orchestrated them using playbooks now even though at the beginning it seemed like if you start to do and so the labels made it so easy so thank you guys for joining today's session I hope that you have learned something useful with this and if you have any kind of questions or if you want to give me any kind of suggestions please feel free to comment and I'll see you next time until then happy learning thank you I hope you enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply to them at the earliest to look out for more videos in a playlist and subscribe to our Eddie Rica channel to learn more happy learning
Info
Channel: edureka!
Views: 367,071
Rating: 4.8085256 out of 5
Keywords: yt:cc=on, ansible playbook, ansible playbook tutorial, ansible tutorial, ansible tutorial for beginners, ansible training, what is ansible, ansible modules, ansible tower, ansible windows, ansible videos, ansible, ansible commands, devops tools, devops tutorial, devops training, devops edureka, ansible edureka, ansible tutorial edureka, edureka, ansible network automation, devops projects, devops engineer, devops basics, ansible devops tutorial, ansible vs puppet
Id: dCQpaTTTv98
Channel Id: undefined
Length: 61min 42sec (3702 seconds)
Published: Wed Jan 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.