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

  • By
  • January 19, 2023
0 Comment

3. Task: Creating and Deploying Jinja2 Template to Managed Nodes Using Playbook

Hello, welcome to this lecture. In this lecture we learn how we can use template module to deploy Ginger to template file, to remote nodes or to remote node. Here is task description create a template hostname dot j two. Here we are using j two extension for Ginger two template file but this is not mandatory but this is recommended to use this extension with Ginger two templating files to differentiate these files from other files. Now, coming back to task create a template hostname j two to generate list of hostnames for all manage nodes. Then we need to create a playbook named hostname dot YML to deploy hostname dot j two Ginger two template to prod one nodes to this file host underscore names under root directory.

So here what we need to do. First of all, we need to create one file hostname j two using Ginger two templating language. In this file we will be using Ginger two for statement and also we’ll use host bars to access or to refer to ansible facts. So here in this task we need to refer to host name. This is four statement four host enunciable underscore play underscore batch. We know how to use this statement, we already discussed about this. Then here we need to specify Ginger to expression.

To refer to host name, we know we need to use hostwars magic variable which contains ansible facts of all active remote nodes in the playbook we’ll use hostwars with host variable which is defined in this statement. Then here we need to refer to the host name. We know this expression here, you might have noticed, I’m not using double quotation marks around double curly brackets, so it’s not needed here.

So whenever you’re using Ginger two expression to refer some variable within some string or in statements, you really don’t need to enclose this expression in double quotation marks. But in case of playbooks, when you are setting some directive using Ginger to expression, you must enclose the expression in quotation marks. So this is very important. But in case you will use double quotation marks here as well, it will note harm. Then in the last we need to end the statement. So after we created this Ginger two templating file, we need to deploy this file to the prode nodes to this file. For this we’ll create one playbook and here we’ll set hosts to all that become must be set to true. Because we need to deploy this template to the host underscore names file under root directory.

So we need root privileges gather underscore facts must be set to true because we are using ansible facts variable in this task. Then here we have template module by using SRC will provide the path where our templating file is present. Here I would like to tell you one important thing. Throughout the course we created all our files under tasks directory and I used related paths in the playbook for different variable files and even for the playbooks. But in case your ansible config file is present under etc directory or in case you create your ensible config file under etc directory you might need to execute playbooks from different paths. In that case, you must provide absolute path for the files directories you will be using within playbooks.

Also you will need to provide absolute path for playbook itself then here by using SRC I provided this file so this is relative paths we can also mention it like forward slash homeancible, forward slash tasks then hostname j two then test where to deploy this file. We know we need to deploy this file on this path to the file host underscore names under root directory now we need to deploy this file only on probe one nodes so we need to mention when condition when inventory underscore hostname in groups prod one we know this expression. Now let’s move to the ensable control node and start doing this task. Now we are on ansible control node and I’m logged in as ansible user. Before we start doing these tasks, I will display documentation for template module. Here we have description templates are processed by Ginger two templating language so we need to create one Ginger to template file using Ginger two templating language which may include different Ginger two statements or Ginger two expressions to refer variables.

We can create Ginger two templating file to create configuration files on the remote nodes but for this exam we don’t need to know how we can configure different configuration files for different services using Ginja two templates but we must know how we can use Ginger two templates to deploy them on remote nodes. Here we’ll discuss important parameters or directives we need to know backup in case you are changing some config file, you can use this parameter or directive to take the backup of original file before it is modified but we are not using this in our task. Then we have test which is mandatory location to render the template to remote machine where we need to deploy this template we need to provide path in case of config file.

We need to provide path where config file is present or where config file should be present force we can use this directive to replace if the file is already existing by default this is true but we are not using this directive in our case because we are creating new file we are not overwriting anything. Then group name of the group that should own the file or directory we can use in case we need to use this mode to configure permissions on the remote file. We know all this. Same for owner then here. This is important and mandatory path of Ginger two formatted template ownership controller. We will create Ginger two template files on ansible controller itself. But they will be deployed to the remote machines with the help of template module. Path can be relative or an absolute path depending upon from where you are executing your playbook I already explained you about this.

We can use validate directive in case we are changing some important config file on the remote node. Then here in the end we have some examples. So here we can see example by using SRC will provide path where Ginger two templating file is present. Which can be relative or absolute path depending upon the location from where you are executing your playbook test. This is a path on the remote node and we should provide always absolute path. Then we have different other directives or parameters. We will not be using all this in our task. This is all about template module. Now I will quit the next task is to create Ginger two template file with name hostname j two. We need to create this file under Tasks directory we can create file anywhere but in that case we must provide absolute path for this file. So here we need to specify our statement. We know we need to use four statement. Because we need to refer to the host names of oval manage nodes. I will change to insert mode here curly, bracket, then percentage symbol for hosting ancient ball underscore play underscore band h, then dash, then percentage, then curly bracket, close, then here.

This expression must be indented to left. Here we’ll use ginger to expression to refer to host name of different variables we know here we must use hostwars magic variable host VARs. Then in the scale brackets we need to use variable we used in this statement. We know how this work. Now here we need to specify expression to refer hostname ansible underscore facts then here hostname we know we need to use bracket notation. Then we must close curly brackets now here we must end four statement and four this is all we need to do for this task. Now I will save and create. Now we need to create one playbook with name hostname dot YML. We must set target as all remote nodes. Because we need to refer to the host names of all remote nodes become must be true. We know why gather facts must be set to true although this is default, I always use this in the playbook. Then here we have Tasks section I will provide some description deploying template. Here we know we need to use template module template. Then here we need to use SRC to provide path where Ginger two templating file is present. In this case I will provide absolute path. And here we have our file.

Then test on the remote node which is forward slash root host underscore names. Then here we must use when condition. Because we need to deploy this template only on prode one nodes means omhost one we’ll use when inventory underscore hostname in groups. And here we’ll specify group prod one this is all we need to do for this task. Here we did two tasks. First we created Ginger Two Templating file then we created one playbook to deploy this Ginger Two Templating file to remote node which is prod one node. Now I will save and quit. But before saving and quitting. I would like to tell you this file name does not exist on the remote node. But same will be created. We don’t need to create this file. I will perform syntax check. Everything seems okay now. We’ll execute this playbook gathering Facts this task is in progress here we can see this task is failed with error and simple undefined variable. For such error, we must check if we used correct syntax to refer variable.

Also we need to check if we did not make any spelling mistake in our case this is spelling mistake so we must correct this here I will open this file again. We know we need to change here this is all we need to do now. Again we’ll execute this playbook. This time everything should be okay. Gathering facts now everything is okay. So this template file has been deployed on Mhost One, which is Prode One node. Now we’ll verify using ansible ad hoc nsible m host one. Then here we need to provide command to display host underscore name on remote node, which is mhostone. We know this file is present or created under root directory.

We must provide complete or absolute path here and we must use become so here we have list of host names so this was objective of this task they can be displayed in any order depending upon the order in which loop is executed by NCBL. But this is not really important for this exam now what I will do, I will clear the screen again I will open this file and here I will make one change. I will remove this dash. Now again, I will execute this playbook just to show you difference of using dash task is in progress. Now again we see yellow output so it means some changes are done. Now again I will display this file. So here you can see this space in front of host names so by using dash we can strip of newline corrector that occupies space in the output again, I will change this file. And I will use here here as well I will execute compute playbook again so we have output again we’ll display this file. Now we have hostname aligned to the left so this is all about this.

4. Task: Creating Jinja2 Template to refer to Partition Sizes & Deploying Template

Hello, welcome to this lecture. In this lecture we learn how we can create Ginger two templating file to refer to sizes of different partitions on remote nodes and how to deploy Ginger two templating file to remote node or nodes. Here is task encryption create a template disk dot j two to generate list of sizes of SDA four, SDF five and SDF six partitions four role manage nodes. Create a playbook named disk YML to deploy this templating file to pro to node to this file disk underscore size under root directory. We know there is one host in pro to group which is m host two. Hostname must be displayed to differentiate. So here we need to display hostname to differentiate which partition belongs to which host. Also here I would like to add we must use Ginger to filter to display some message in case partition does not exist. For example, we know sta six partition does not exist on am host two.

So in that case it will display node applicable. We are using Ginger two filter now coming to Ginger two template file, we need to use named disk j two. Again, here we’ll use four statement. So for the exam you must know how to use four statement four host in ansible underscore play underscore batch we know the syntax. Then here I wrote size of sta four on host enclosed in double curly brackets. So here I’m referring to host variable for first iteration. For example, it will be replaced with m host one, then equals to sign. Then here I’m referring to the partition size of SGfour partition. We know how to form this expression. Important here is we must use hostwars because we need to use ansible facts of other nodes on every node host VARs then in the bracket variable we are using here. Then here we need to provide expression for size. So in first case this expression is referring to size of SDF or partition. So if this variable value is note present of this variable does not exist, then by default note applicable or NA should be displayed.

Then we have next line for SDF five partition. Again everything is same. The only difference is we must specify SG five here same for Sdsx. Then we must end for loop. Now, during different iteration, this host variable will be replaced with m host one, m host two, m host three and m host four and corresponding sizes will be displayed. Then moving to the playbook contents here we must set target as all managed nodes. Because we need to display partition sizes of all managed nodes. Then become must be set to true because we are deploying or we need to deploy this template to some file under root directory. So we need root privileges gather underscore facts that must be true because we are using ancible facts variable. Then we know to deploy template, we need to use template module using SRC will provide absolute relative path of Ginger two templating file. Then using test we’ll provide path on the remote node where this template should be deployed on.

Then finally using ven condition I am restricting execution on pro to nodes which is task requirement. This is all about task description. Now let’s move to the ensable control node and start doing this task. Now we are on ansible control node and I am logged in as ansible user here on this path we need to create Ginger two templating file. We know we need to create Ginger two templating file with named disk j two. Here we’ll write our four statement. We know the syntax for host in ansible underscore play underscore batch. Here we must use dash percentage then end with curly bracket. Then here I will write text size of SDF four on host. Here we are using Ginger two expression. We know why we need to use this variable here. Just to differentiate between the sizes of different partition. We can also enclose this expression in double quotation marks.

It will note harm but it’s up to you how you want to display this in the output. And also it depends on the task requirements. Then equal to then here we know we need to provide expression to refer size of Sour partition. So this is expression we need to specify to refer to the size of SDF four partition. Now here we need to use Ginger to default filter. We know how to use it and what is purpose of using this now double curly brackets close don’t forget this. Now we need to write the same line for staf five dust six. I will just copy this and paste this two times. Now I will make required changes. Staf five then here it must be sat five. STF six. Then here it must be st six. Now to move to the end of line I will use capital A. Here we must end our four statement and four percentage bracket curly bracket close. This is all we need to do to create this templating file. I will just check the contents of this file to make sure we did not make any mistake. Everything seems okay.

I will make one more change here I will just enclose these expressions in quotation marks. Same in the beginning just to format the output in readable format now I will save and quit. Now we need to create playbook to deploy this templating file to pro to node we need to create playbook with named YML. We must set target as all nodes then become must be set to yes or true. Gather facts must be set to true. Then in the tasks section will define our task. I will provide some description here. Deploying Template we know we need to use template module by using SRC we specify Ginger two templating file here. In this case I’m using relative path then test root, then file name is disk underscore size. Now we need to use when condition to restrict the execution of this task only on pro two nodes when inventory underscore hostname in groups. Pro two this is all we need to do now. I will save and quit before execution of playbook. I will perform syntax check.

All seems okay. Now we’ll execute this playbook again I will mention here I am providing only relative path for the playbook. In case your playbook is existing somewhere else, you must provide absolute path for playbook execution. Gathering facts. This task is in progress. So these three nodes are skipped. Template has been deployed to file on Most two which is pro to node. This is as expected. Now I will clear the screen. Now we know to verify, we’ll use ensible ad hoc command ensible m host two. This time target is Most true. Here I will provide command to display file on m host two. We must use become flag. We know why. Here we can see output.

We see sizes of different partitions are displayed as per our template. Because we enclosed the expression in double quotation marks. So we have these double quotation marks here. We used space in the template here, so we have space. So in this way you can format according to requirement. We also have hostname here. Now we know this is partition belonging to m host three. And this is partition belonging to m host four. Here we see size of SDS six on m host two. Not applicable because this partition is not created on m host two. So this is as expected. And this is objective of using Ginger to default filter. This is all about this task on the exam you must know how to form expression to refer to different variables or I will say to refer to different ansible facts variable. You will need to form different expressions for these kind of tasks. This is over.

Comments
* The most recent comment are at the top

Interesting posts

The Growing Demand for IT Certifications in the Fintech Industry

The fintech industry is experiencing an unprecedented boom, driven by the relentless pace of technological innovation and the increasing integration of financial services with digital platforms. As the lines between finance and technology blur, the need for highly skilled professionals who can navigate both worlds is greater than ever. One of the most effective ways… Read More »

CompTIA Security+ vs. CEH: Entry-Level Cybersecurity Certifications Compared

In today’s digital world, cybersecurity is no longer just a technical concern; it’s a critical business priority. With cyber threats evolving rapidly, organizations of all sizes are seeking skilled professionals to protect their digital assets. For those looking to break into the cybersecurity field, earning a certification is a great way to validate your skills… Read More »

The Evolving Role of ITIL: What’s New in ITIL 4 Managing Professional Transition Exam?

If you’ve been in the IT service management (ITSM) world for a while, you’ve probably heard of ITIL – the framework that’s been guiding IT professionals in delivering high-quality services for decades. The Information Technology Infrastructure Library (ITIL) has evolved significantly over the years, and its latest iteration, ITIL 4, marks a substantial shift in… Read More »

SASE and Zero Trust: How New Security Architectures are Shaping Cisco’s CyberOps Certification

As cybersecurity threats become increasingly sophisticated and pervasive, traditional security models are proving inadequate for today’s complex digital environments. To address these challenges, modern security frameworks such as SASE (Secure Access Service Edge) and Zero Trust are revolutionizing how organizations protect their networks and data. Recognizing the shift towards these advanced security architectures, Cisco has… Read More »

CompTIA’s CASP+ (CAS-004) Gets Tougher: What’s New in Advanced Security Practitioner Certification?

The cybersecurity landscape is constantly evolving, and with it, the certifications that validate the expertise of security professionals must adapt to address new challenges and technologies. CompTIA’s CASP+ (CompTIA Advanced Security Practitioner) certification has long been a hallmark of advanced knowledge in cybersecurity, distinguishing those who are capable of designing, implementing, and managing enterprise-level security… Read More »

Azure DevOps Engineer Expert Certification: What’s Changed in the New AZ-400 Exam Blueprint?

The cloud landscape is evolving at a breakneck pace, and with it, the certifications that validate an IT professional’s skills. One such certification is the Microsoft Certified: DevOps Engineer Expert, which is validated through the AZ-400 exam. This exam has undergone significant changes to reflect the latest trends, tools, and methodologies in the DevOps world.… Read More »

img