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

Preparing for Juniper Networks JNCIA-Junos Exam: Key Topics and Mock Exam Resources

So, you’ve decided to take the plunge and go for the Juniper Networks JNCIA-Junos certification, huh? Great choice! This certification serves as a robust foundation for anyone aiming to build a career in networking. However, preparing for the exam can be a daunting task. The good news is that this guide covers the key topics… Read More »

Mastering Microsoft Azure Fundamentals AZ-900: Essential Study Materials

Ever wondered how businesses run these days without giant server rooms? That’s the magic of cloud computing, and Microsoft Azure is a leading cloud platform. Thinking about a career in this exciting field? If so, mastering the Microsoft Certified: Azure Fundamentals certification through passing the AZ-900 exam is the perfect starting point for you. This… Read More »

The Impact of Remote Work on IT Certification Exam Processes

With remote work becoming the new norm, it’s not just our daily routines that have changed but also how we tackle IT certification exams. Gone are the days of trekking to testing centers; now, your living room can double as an exam room. This shift has brought about some fascinating changes and challenges. Let’s dive… Read More »

IT Risk Management: CRISC Certification Exam Essentials

Do you ever feel like the IT world is moving at warp speed? New tech seems to pop up every day, leaving you wondering how to keep up and truly stand out in your field. Companies are increasingly concerned about online threats, data leaks, and meeting legal requirements. That’s where the CRISC (Certified in Risk… Read More »

The Ultimate Guide to Mastering Marketing Automation for Email Wizards

Hey there, email aficionados! Welcome to your new favorite read – the one that’s going to turbocharge your email marketing game. You’re about to dive into the captivating world of marketing automation, a place where efficiency meets effectiveness, letting you boost your campaigns without breaking a sweat. Get ready to discover how automation can not… Read More »

Master YouTube Marketing with These 10 Powerful Steps

Welcome to the dynamic world of YouTube marketing! Whether you’re a seasoned pro or just getting started, harnessing the power of YouTube can significantly boost your brand’s visibility and engagement. With over 2 billion monthly active users, YouTube offers a vast audience for your content. But how do you stand out in such a crowded… 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/    |