Ansible Host Variables and Group Variables | #Ansible #FullCourse | techbeatly

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone so in the last video we learned about the variables in ansible and also we learn about how to manage this variable inside the ansible playbook also outside the playbook using the variable files so you know how to use the variable inside the playbook and how you can use the variables inside or outside the playbook inside a variable file if you have some specific need for defining the variables together with ansible inventory let's say you have some host specific or group specific variable then you can use a functionality in sensible called host variable and group variable so we have this uh existing playbook we are going to use the same playbook for the demo so c variable demo so we have these files already uh you can refer the this video description for the github repository and take the file from there or if you have already the one from your previous uh lab then you can use that okay we have this ansible dot cfg with a minimal configuration and you can see there is a remote user devops i will explain that then we have a inventory with the two nodes and a localhost then we have a sorry we have a watts files which contains some webpack keys and the firewall package to be installed also we have the main playbook which will install http or web package start the web package install firewall enable the firewall port all those things okay it's the same playbook is a just for the demo okay so we know the variables you can keep inside the playbook outside the playbook and this is about host variable let's say inside your inventory so we have this inventory and we have two nodes in case let's say i have a different uh username for different nodes and i need to use that specific to the host right so what you can do here we are going to define a variable for node two and node one so i am mentioning okay and symbol from user equal to devops so ansible hyphen user is a default variable and we are declaring this inside the inventory as devops okay and ansible iphone user equal to vagrant so i want to use different users for different host this is just an example let's see what will happen so we have ansible nodes i'm using just a shell command and checking and maybe id let's see so ansible run on note 2 with the wagon user and on node 1 with a devops user hope this is very easy one so if you don't understand please ask in the comment box so inside the inventory file we have mentioned which ansible user to be used so you can use any variable specific to the host like this okay get inventory see uh node one is using devops yes node one is using devops user and node two is using vagrant user okay even we have this sorry and simple.cfg we put remote user but the host variable already taken more precedence and it's used okay this is uh the inventory variable let's say if you have more and more variable it won't be nice to keep everything inside the inventory instead of keeping everything inside the inventory we want to keep this variable under an organized file structure and that is called host variables and group variables so what we have to do inside your project directory so this is our project directory we have these files uh we have to create some directory structure okay before i go to the host and group variable so i will do one more thing we go to the inventory now we have nodes and we have node 1 and o2 let's say i want to define some specific variable for the entire nodes group so what i can do nodes i will forget this forget this otherwise notes and you mentioned words here you can define variables specific to the node group so in this case whatever nodes under the nodes group will be assigned with this variable for example if i use mcable underscore user equal to double here what will happen so which variable will be assigned okay we can try so we are using the same share and see what will be the effect did you see anything because a host variable will have more precedence and even you mentioned group variables as devos but the host variables you mentioned as vagrant it will take over please remember this variable precedence so if you go and check ansible variable [Music] sorry for the typo let's see the documentation on the order or precedence of ansible variable so if you go down you can see a link here variable presence so you have so many places uh to keep your variables and depends on the location the presence will change so if you go down you can see around 22 position you can define variables so you can see command and variable like minus u minus user these are not variables but it's a parameter then we have raw defaults inventory files so this is the one we are talking about the group inventory variables inventory host wars play play book variables and others and you can see the last one i mean the 22 will have most uh preference and you can see extra watts for example whatever you pass to the playbook will win distance okay remember this order because i got so many uh complaining that okay they have defined this variable here and here but it's not really working of course it won't work because it will take variable value from different location based on the precedence okay you can read more about the president's in the documentation later okay now we know this one okay now we have this uh issue called let's say we have more and more variables or more and more details to be keep inside the inventory than the inventory won't look nice and it won't be it will be very difficult to keep this inventory clean so what you have to do is inside the inventory we will remove these variables and we will keep this under specific directory okay so uh so this is a recommended wave okay so we will create that one what you have to do we will create a directory called mkdir group underscore wars then mkd ar host this forwards now you can see we have a new directory called group wars and host wars okay now we have to move these items to the current location and now we have these items inside the inventory so we have to move this variable to the proper location so what you have to do bim host bars my node is node one so use the node name for the file name under the hotspots number one insert and symbol underscore user then here you use the column remember that don't use equal to symbol so ansible user uh devops okay good now we have node 2 and simple underscore user because these are variable files vagrant okay now use three okay we have two files here then we have to create the hostwars right so we sorry group wars group or then our group name is nodes nodes then okay we are not going to uh use this variable because reason you learn already i will remove this okay so the inventory is clean no variables inside now we have to use something for the demo purpose the group okay we have this called um cat uh web package uh defined inside the watch file i don't want to use the watch file instead for these nodes i want to use uh this this variable inside the host group bar so what i'll do group parts nodes web underscores package okay maybe we can do something like cat wars 2 group wars notes okay okay so we have this nodes file cat group ours notes okay the content is there so we have uh now we are going to edit our site.ml now we are using this variable from whatsfire so we are not using the wire files anymore uh instead we are going to use the hostwars okay so we will check now uh first we will try the shell command whether it's working or not yes it's taking because one user taking vagrant and one other one already develops so we are not going to use the variable files instead we are going to use the hostwars and group words so first thing uh ansible playbook cy.ml and minus e we have to give this because we already mentioned inside the extra words notes equal to nodes it's running okay node one node two all good okay good now we have two servers installed with uh http server and uh firewall all good okay this is very simple one let's say you want to check like something you want to try with the different variables different host so what i can do first we will remove the httpd from the target machine so nc reduce a command like ansible sm okay we have to give notes and this m minus a then name equal to http t equal to absent so we are going to remove this okay we are to give becomes orange okay we will go and check so we are trying the shell module so we will try okay you can see there is no matching package listing this is not installed okay now we have to try with a different variables for different notes inside the host wars so cat group wars notes we have these packages but the default it will be using http but i want to try something like bim postwars node one i want to use web package web package nginx i don't use nginx and web underscore service also nginx so i am going to use web package engine x and services nginx for node but for node 2 i'm going to keep the same and i'm not going to override okay now here you can see the difference here uh it's going to install nginx and one http let's see what is happen so here we go and check ssh node one sudo um let's install http no package on note one okay engineering system sudo [Music] system ctrl engineers status engine x okay good node one engine x is installed now you have to check node two sudo um list installed nginx i think okay note 3 is using http okay sudo m list installed sorry so system ctl status http good so we have one machine installed with http and the other machine installed with nginx you're using the same playbook you didn't change anything inside the playbook and instead of using the watchbar we use the hostwars so you can use a variable like this if you want to check okay node one okay there is some default file girl no two uh there is something actually the nginx one we didn't configure the default html just ignore that one at least you know how to handle these group awards and host wars and that's it for this video please refer the documentation if you have any questions and also ask in the comment if you want to clarify anything and please watch other videos in the ansible series to get more idea on these variables and thank you for watching please subscribe to this youtube channel to get more videos like this and see you next time with another video thank you you
Info
Channel: techbeatly
Views: 7,924
Rating: undefined out of 5
Keywords: ansible introduction, what is ansible, ansible full course, ansible automation training, ansible training free, ansible training, learn ansible step by step, learn ansible, learn ansible tower, ansible tutorial, ansible tutorial for beginners, ansible playbook tutorial, how to learn ansible, ansible course, ansible ex407 course, ansible ex294 course, everything about ansible, best ansible course in youtube, free ansible training in youtube, ansible for absolute beginners
Id: l_dxJdgLbyw
Channel Id: undefined
Length: 16min 53sec (1013 seconds)
Published: Thu Apr 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.