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

  • By
  • January 19, 2023
0 Comment

1. Understanding jinja2 if statement

Hello, welcome to this lecture. In this lecture we’ll discuss about Ginger Two template if statement. We will be using Ginger Two if and for statements later on, while creating Ginger Two template files. So this is important to understand them. We’ll learn how two if statement works with the help of one example. Here we have example target is set to all. We’ll set gather underscore facts to false. We don’t need them. In the tasks section we’ll use debug module to understand if statement. Here you might have noticed this greater than symbol. So this is needed. Because we need to provide multiline string which we need to define our if statement. Now coming to this statement here, first line is command line, curly brackets, hash. Then write some command, then hash and curly bracket close here I used this dash symbol. This is to strip new line character in the output. It will be more clear later on. For now we need to just keep in mind. Then here we have if statement.

So this is syntax of if statement in Ginger Two templating language, curly bracket. Then percentage sign. Then here we need to specify our statement if inventory underscore hostname in groups web servers. We know this expression. We are using magic variables here. So here important is don’t enclose any variable name in quotation marks. For example inventory underscore hostname here. Otherwise it will not work. And finally you must close the statement with percentage and curly brackets. Again, I’m using dash symbol here. So if inventory underscore hostname in groups web servers then display message. This is web server node. Then here we have next statement using Lyf. Again we need to use same syntax, curly bracket, percentage sign in the beginning and in the end. And also symbol. Here we have lliif inventory underscore hostname and groups prod one. Then display this message. This is prod one node. Then finally we have else.

Again use same syntax. And here we need to display message. This is pro two node. Then in the end you must end if statement using and if using same syntax. So here what will happen if inventory underscore hostname is in groups web servers. This message will be displayed. We know this message will be displayed for Mhost three and four. Then here in next condition it will check if inventory underscore hostname in groups prod one. It will display this message. This will be displayed for m host one. And finally this message will be displayed for Mhost two.

Now let’s move to the enslave control node. And understand how this works practically. Now we are on ansible control node. And I am logged in as ansible user. Here we’ll create one playbook to understand working of Ginger two if statement. I will create playbook with name j two if YML. Here we need to set target as all remote nodes. I will set gather facts to folds. We don’t need them. Then here in the tasks section. We’ll define our task using Ginger Two if statement. Here we need to provide some description. I will use J two if statement. Then we know here we need to use debug module. We’ll use MSG parameter. And here we need to use greater than symbol. Now we know what we need to do. First line command line write something here. J two if statement, you can write whatever you want to write here.

Then we know we need to close this statement with same hash curly brackets I’m using in front of this. Then here is statement which will start with curly brackets. Then percentage symbol if inventory underscore hostname in groups web servers. Then here I will close this statement. Here we need to write our message. This is web server node. Then here we need to write our next statement using alif curly bracket percentage symbol alif inventory underscore hostname in groups prod one. Then we need to close this statement. Don’t forget this. Here we’ll display message. This is prod one node. Then finally we’ll write else statement. Curly bracket percentage symbol else then here we need to close the statement else print. This is pro two node. And finally, don’t forget to end if statement. And if I will use same syntax. This is all we need to do.

Three dots indicating end of YAML document. Here important is you must note use quotation marks while using variables. I will save and quit. First of all, we’ll check syntax of this playbook. All seems okay. Now we’ll execute this playbook NCB dash playbook. Then playbook name. Here we have message for m host three and four, we have message. This is web server node with new line character in the end, same four m host four. For m host one we have message. This is prod one node. And finally we have this message. Due to l statement this is pro two node. So this is how we can use Ginger two if statement. Now I will clear the screen again. I will open this playbook. And we’ll make some changes. Here, instead of using magic variables, I will use ansible facts. Ansible underscore hostname. I will clear all this. Then double equal to sign for equality.

And here we need to provide M host one. We must enclose this value in double quotation marks. Because this is not variable. If ansible underscore hostname is equal to mhstone display message. This is prode one node. Then I will change here. Alive nsible underscore hostname will set this to Mhost two. I will remove all this. And here we’ll display message. This is pro two node. And finally using else will display message. This is web server node.

So here we must set gather underscore facts to true. Because we are using ansible facts here. Again, don’t enclose this variable in quotation marks. But you must enclose values in quotation marks as I did here. So we will check if hostname is m host one. Display message. This is Prod One node. L. If hostname is Mhost Two, this is Pro Two. Node. Else. This is web server node. Now I will save and quit. We’ll execute this playbook. It will take little longer here. Now we have message. For M host Three and four, we have message. This is web server node. And for M host One, we have message. This is Prod One node. And for M host Two this is Pro Two node. As expected, this is how we can use Ginger Two if statements. This is all.

2. Understanding jinja2 for statement

Hello, welcome to this lecture. In this lecture we’ll learn about Ginger to template four statement. We will understand this with the help of one example. Here we have our example playbook. We’ll set target to all using host set to all get other underscore facts must be set to true. Again in the task section we’ll define our task using Ginger to four statement. Again we need to use debug module and we’ll use greater than symbol to provide multiline string to define full statement. This is command line syntax is same as we discussed earlier for if statement. So here we have four statement for host in ansible underscore play underscore batch. Here we are using host as variable. Again, don’t enclose any variable in quotation marks in these statements. Then we have this magic variable. We know this magic variable stores all the active hosts in the play. So in our case we set or we’ll set target to all. So it means it will store all the managed nodes starting from m host one to m host four. Then here we have expression to display host name for each host.

This is important. Here we are using host valves which is magic variable. And by using this variable we can access or use facts of other managed nodes. So what we are doing here in the scare brackets we are using host variable we used in this statement. And finally here we are using bracket notation with ansible underscore hostname to refer to hostname of host. Then here this is and of this statement in this case and four syntax is same.

So what will happen? During first iteration, this host will be replaced with m host one. Then we know hostwars contains Ancill facts of all managed nodes. So it will display host name for moonstone. During second iteration, this host variable will be replaced with m host two and hostname for Mhost two will be displayed same for Mhost three and m host four. This is how by using host bars we can access or use information of other managed nodes.

Now let’s move to the ensible control node. And understand how this works practically. Now we are on ansible control node and I’m logged in as ansible user. Here we’ll create playbook with name j 24 YML will set target to all gather underscore facts must be set to true because we are using ansible facts during this task. Then in the tasks section will define our task. Here we need to provide some description j 24 statement.

Here we’ll use debug module and we know here we need to use MSG. Then greater than here we’ll write our statement. First command line in case you want to specify this g two four statement we must close this. Then here we’ll write four statement curly, bracket percentage sign four host in Ancill underscore play underscore batch. Then here we’ll close this statement. Now what action is needed? We want to display hostname. So this expression we are going to use here must be indented to left we know how to refer to host name. Here we’ll use host bars which is magic variable in the scale brackets will specify variable we used here in first statement don’t enclose this variable in any quotation marks otherwise it will not work. Then here I will provide expression to refer to hostname. We can do it in two ways. First we’ll write an sybil underscore hostname. Of course we can use dot or bracket notation here then close this with double curly brackets.

This is something we know already. Then here we need to end four statement and four. This is all we need to do for this task or for this example now I will save this and quit. But before that I would like to explain what will happen when we execute this playbook. For every host this force statement will be executed. And we know hostwas contains ansible facts of all the variables. During first iteration it will display ansible hostname for m host one then two then three and finally m host four and this will be executed on each node. And finally for each node we will have host names of all remote nodes in the output now we’ll save and quit. I will perform syntax check all scenes.

OK, now we’ll execute this playbook. It will take little longer because we need to gather facts. And here we have output as I told you earlier for each host we’ll see list of host names because this for loop is executed for each manage node. So in this way we can access information of other node on any manage node. Now I will clear the screen again we’ll open this file and here I will make one change nsible underscore facts. Then here we can write hostname using bracket notation. So this will display same thing we can refer to hostname in two different ways.

We know this already. Again we’ll execute this playbook to see if this works fine we have same output this is all about this task. This is how we can use Ginger two four statement. In next task we will be using same statement everything same with template.

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 »

sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |