EX294 Red Hat Certified Engineer RHCE – Using Advanced Ansible Features – Ansible Vault and Templates Part 3

  • By
  • January 19, 2023
0 Comment

5. Task: Creating J2 Template to Configure Hosts file as Local DNS on Managed Nodes

Hello, welcome to this lecture. In this lecture we learn how we can configure hosts file on the remote nodes as local DNS using Ginger to template file and template module. This is practical example and we often need to configure host file to attend as local DNS. Here is task description create a playbook named hosts YML to configure local DNS functionality on all remote nodes or hosts using hosts file. Use hosts j two as template file and hosts file contents should display IP address then Sqdn, then hostname. We know format of hosts file default entries for local host should also be included. We know in every host’s file there are always default entries for local node.

They should note be overwritten, we must include them as well. Now coming to Ginger two template file again here we need to use for statement. We know how to use it for hosting and siebel underscore play underscore batch. Then here I am also adding one if statement. However, we don’t need this. Because we know this interface or this device is existing on all our managed nodes. You might not need the same on the exam.

So here we are checking if ansie bell underscore ENP zero three. This is interface name device name. In our case on the exam, you might have different interface name. For example ETH zero. So if this interface is in host bar host so it will check ansible facts of each host. And if this interface is present then it will move to this statement. But we will emit this from our file. We really don’t need this. Then here is the expression to refer to the IP address. We know how to form this expression. Important here is we must use host words along with host variable.

Then we know this expression. Then tab then here I’m specifying host using Ginger two expression. We know this will be replaced with m host one, two three and m host four during different four iterations. I’m adding dot example to display or to use Sqdn or to add Sqdn and finally host.

Then we must add if statement then for statement this is all we need to do to create Ginger to templating file. Now coming to the playbook contents we must set target as all remote nodes become must be set to true because we need to configure host file which is system administration task gather underscore facts must be set to true because we need to use ansible facts variables during this task.

Then here in the tasks section we know we need to use template module using SRC will specify Ginger two templating file. Then here is test. We know this test etc hosts. Here we don’t need to use any when condition. Because we need to configure host file on all remote nodes. Now let’s move to the ansible control node and start doing this task. Now we are on Ensile control node and I’m logged in as ansible user on this path we need to create Ginger two templating file.

Also we need to create playbook. But first of all I will display contents of hosts file on all remote nodes. We know we can use ansible ado command for this ansible old then here we need to provide command to display file. This is all we need to do. So here we have output on each host. We have only default entries. Now I will clear the screen. Now what we’ll do we’ll copy host file present on and symbol controller node to our file which we need to create as template file just to copy to default lines hosts dot j two now I will open this file in editing mode. We know we need to delete all this, we just need to keep these two lines. Now here we’ll write our four statement. We know how to write four statement for hosting ansible underscore play underscore batch then dash percentage, curly, bracket close here I will provide expression to refer to IP address.

So we need to use this expression make sure you are using correct name for interface. Now I will close this with double curly brackets tab now here we know we need to use host then dot example then tab then here we’ll use host. We must provide space in the beginning and end. This is all we need to do. As I told you earlier, I am omitting if statement because this is not needed in our case. And four you can practice using if statement as well but this will not bring any difference in our case. Now I will just go over this file just to make sure everything is okay and we did not miss any brackets all scenes okay on the top I would like to add one more line.

For example managed by Ncbull just to give indication this file is created using Ncbull I will save and create. Now we need to create our playbook with name host dot YML we must set target as all become must be set to true. Gather text must be set to true. Then in the tasks section will define our task. I will provide description deploying template we know we need to use template module using SRC I will specify Ginger two templating file then test forward, slash etc hosts this is all we need to do. I will save and exit. Now before we execute this playbook, I will perform syntax check olsen OK, now we’ll execute this playbook enslave host YML gathering facts. This task is in progress. Deploying template template is being deployed. It has been done.

We have good sign. Now we know we need to use add hook command to verify results. This is all we need to provide here. So here we have output as expected on every node this file has been configured. We can see our command on the top just to give indication this file is managed by an sibal. We also have default entries. Then we have our entries in the format as per requirement. I will tell you again the order of these entries can be different. This does not matter for the exam at all. You don’t need to worry about this. But we have correct entries. This is IP address of m host one. This is Sqdn. Then hostname same for all nodes. This is all about.

6. Task: Creating J2 Template to Gather and Save Information About Remote Systems

Hello, welcome to this lecture. In this lecture we learn how we can access information about remote systems using Ginger two templating file. Here is task description create a template info j two to gather and save below information about remote system in specified format, we need to access host name variable for different managed nodes. We also need to refer to size of volume group waygroup. And then we’ll display size for each remote node same for size of logical volume LVM. And also we need to access ansible OS family fact variable. And we need to display variable value for each remote node we know. To refer to all these variables, we’ll create Ginger two templating file with name info dot j two then we need to create a playbook with name info dot YML to deploy this template to info file under root directory on each remote node. So here I would like to tell you one important thing. This task is different from the tasks we discussed already. Here we don’t need to access information about other managed nodes on any managed node.

Here we are referring or accessing ansible facts variables of remote node itself. So here we are not going to use for loop and host VAS magic variable. So on the exam you must keep in mind when you need to use for loop and hostwars only the case where you need to access ansible facts variable of other managed nodes on manage nodes or manage node. But this task is simple. We’ll just create one file with name info dot j two. Then we know this template in this template will provide Ginger to expression to refer to these variables, we know how to form these expressions. So this is for hostname, this is for size of volume group vice of logical volume LVM. I’ll show you when we move to the system or ensible control node, this expression is to refer to ansible OS family. So this is simple key value pair. This is key to display OS family. Then we’ll create one playbook.

We’ll set target as all manage nodes become, must be set to true. Because we need to deploy this template to some file under root directory and we need root privileges gather underscore facts must be set to true for all these kind of tasks. Then in the tasks section we’ll use template module using SRC directive will provide relative path for this templating file info dot j two you can also provide absolute path. I already explained this when you need to provide absolute path, but absolute path will work always. Then in the desk we’ll provide where we need to deploy this template on the remote nodes. Here we need to deploy this template on each managed node. So we don’t need to use when condition to restrict execution of this task for specific remote node. Now let’s move to the ensuile control node and start doing this task.

Now we are on ensible control node and I am logged in as ansible user here under Tasks directory we need to create Ginger two templating file info dot j two. But before we create this file, I will display ansible facts for M host one. Just to understand how to form expression to display or to refer to the sizes of volume group and logical volume. I know you know this already. But this time I will use filter parameter while displaying facts. Just to filter the info we need, I will display sensible facts for Mhost one. So this time I will use a option with setup module to provide arguments. We know how we can do this. I will set filter parameter to ensure underscore LVM so that information under this dictionary variable will be displayed. I will press enter.

We have nothing in the output, but we know we have logical volume group and logical volumes created on this remote node. Then why nothing is shown? This is because of we did not use Become. So this is important for you to know. You can display this dictionary variable only with root Parabella JS. So here we have info, this is top level dictionary variable Ansible underscore facts. Then under this we have ansible underscore LVM. We know we can use ansible underscore facts as to a playable dictionary variable than LVM. We cannot use ansible underscore two times in expression. Now under LVM we have LVS dictionary variable containing information about logical volumes. And we have VGS dictionary variable containing information about volume group. Now, to refer to the size of volume group VGROUP, we know what we need to do. Under LVM we’ll go to VGS, then VGROUP, then size underscore G. So in this way we can refer to the size of volume group.

We also need to refer to the size of logical volume LVM. For this expression will be an sibling underscore facts then LVM, then we have LVS. Then this is name of logical volume we created. So in our case we will be using LVM two times in the expression. But on the exam you might need to create logical volume with different name. In that case, so this name will be or this name could be different. Then under this we have size underscore G. So using this key will display or access this size. I believe at this moment you know how to form these expressions. But I just wanted to tell you how you can use filter directive to filter ansible facts output.

This is very useful when you know ansible facts variables you need are present under which dictionary variable which in our case is ansible underscore LVM. But in case you don’t know where your ancient facts variables are present, in that case you must display or you will have only option to display all fax variables and then search for the variable you need. Now I will clear the screen now, next we need to create Ginger to templating file to refer to the different variables we need for this task we’ll create file with name info dot j two as per task requirements. Here we need to use same format as specified in task requirements. We know this is expression to refer to the host name, we can also use ansible underscore facts then hostname in the scale brackets using bracket notations then next, size of volume group wave group we know which expression to use here.

So this is the expression we need to use here. Here we can specify this expression without using double quotation marks as well. I’m using this because this is part of template as per task requirements. Next is size of logical volume LVM. We know which expression to use here. Here we need to use this expression last sensible OS family here we’ll use this key to access an symbol OS family this is all about this templating file here make sure you did not miss any quotation marks or any bracket. Otherwise you will see errors while execution. Now I will save and create.

Now we need to create playbook with name info YML to deploy this template to all manage nodes we’ll set target as all remote nodes become must be set to yes or True gather underscore facts must be set to true or yes. Then in the tasks section we’ll define our task. We know we need to use template module using SRC parameter we specify Ginger II templating file or path four ginger two templating file I am providing here relative path using dust will specify path on the remote node where this template should be deployed on. So this is the path where we need to deploy this template. Info file will be created on the remote nodes automatically.

So this is all about this playbook now before execution I will perform syntax check. All seems okay. Now we’ll execute this playbook task. Gathering facts in progress. Deploying template now. Here we have error for Mhost two we know why. Because volume group VGROUP is not created on Mhost two. So for this part we must configure error handling. I will again open this file and I will use Ginger to filter. Here we know how we can use Ginger to filter. By default it will display an A in case variable does not exist. So this is all we need to do on the exam. They will provide you with complete details. Now again we’ll execute this playbook task gathering facts in progress deploying template in progress. This time no error. We see changes are done only for m host two. Changes were done already for m host one, four and three when we executed this playbook first time.

This time changes are done for m host two and no errors are shown. So this is as expected. I will clear the screen now to verify, we’ll use ansible ad hoc command using command module ansible target is all remote nodes using dasha I will provide command to be executed on the remote nodes. Here we must use dash. Dash become flagged because this file cannot be displayed without root privileges. So here we have output. We have hostname filled in. Also, we have size of volume group way group, which is one gigabytes for M host four and zero point 58 Gibbytes for Mhost one. For M host two. We have NA for size of volume group VGROUP and size of logical volume LVM. We know volume group is not created on M host two, so this is normal and civil OS family is red Hat. So in this way we can gather info about remote nodes using Ginger two templating.

Comments
* The most recent comment are at the top

Interesting posts

Impact of AI and Machine Learning on IT Certifications: How AI is influencing IT Certification Courses and Exams

The tech world is like a never-ending game of upgrades, and IT certifications are no exception. With Artificial Intelligence (AI) and Machine Learning (ML) taking over everything these days, it’s no surprise they are shaking things up in the world of IT training. As these technologies keep evolving, they are seriously influencing IT certifications, changing… Read More »

Blockchain Technology Certifications: Exploring Certifications For Blockchain Technology And Their Relevance In Various Industries Beyond Just Cryptocurrency

Greetings! So, you’re curious about blockchain technology and wondering if diving into certifications is worth your while? Well, you’ve come to the right place! Blockchain is not just the backbone of cryptocurrency; it’s a revolutionary technology that’s making waves across various industries, from finance to healthcare and beyond. Let’s unpack the world of blockchain certifications… Read More »

Everything ENNA: Cisco’s New Network Assurance Specialist Certification

The landscape of networking is constantly evolving, driven by rapid technological advancements and growing business demands. For IT professionals, staying ahead in this dynamic environment requires an ongoing commitment to developing and refining their skills. Recognizing the critical need for specialized expertise in network assurance, Cisco has introduced the Cisco Enterprise Network Assurance (ENNA) v1.0… Read More »

Best Networking Certifications to Earn in 2024

The internet is a wondrous invention that connects us to information and entertainment at lightning speed, except when it doesn’t. Honestly, grappling with network slowdowns and untangling those troubleshooting puzzles can drive just about anyone to the brink of frustration. But what if you could become the master of your own digital destiny? Enter the… Read More »

Navigating Vendor-Neutral vs Vendor-Specific Certifications: In-depth Analysis Of The Pros And Cons, With Guidance On Choosing The Right Type For Your Career Goals

Hey, tech folks! Today, we’re slicing through the fog around a classic dilemma in the IT certification world: vendor-neutral vs vendor-specific certifications. Whether you’re a fresh-faced newbie or a seasoned geek, picking the right cert can feel like trying to choose your favorite ice cream flavor at a new parlor – exciting but kinda overwhelming.… Read More »

Achieving Your ISO Certification Made Simple

So, you’ve decided to step up your game and snag that ISO certification, huh? Good on you! Whether it’s to polish your company’s reputation, meet supplier requirements, or enhance operational efficiency, getting ISO certified is like telling the world, “Hey, we really know what we’re doing!” But, like with any worthwhile endeavor, the road to… Read More »

img