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

IBM Certified Data Scientist: Building a Career in Data Science

In today’s digital age, data is the new oil, driving decision-making and innovation across industries. The role of a data scientist has become one of the most sought-after positions in the tech world. If you’re considering a career in data science, obtaining the IBM Certified Data Scientist certification can be a game-changer. This certification not… Read More »

How to Balance Work and Study While Preparing for IT Certification Exams

Balancing work and study while preparing for IT certification exams can feel like an uphill battle. Juggling a full-time job and intense study sessions requires careful planning, discipline, and creativity. The pressure of meeting job responsibilities while dedicating time and energy to study can be overwhelming. However, with the right strategies and mindset, you can… Read More »

10 Highest Paying IT Certifications

In the ever-evolving world of information technology, certifications are more than just a feather in your cap – they’re a ticket to higher salaries and advanced career opportunities. With the tech landscape constantly shifting, staying updated with the most lucrative and relevant certifications can set you apart in a competitive job market. Whether you’re aiming… Read More »

Strategies for ISACA Certified Information Systems Auditor (CISA) Exam

Are you ready to take your career in information systems auditing to the next level? The ISACA Certified Information Systems Auditor (CISA) exam is your ticket to becoming a recognized expert in the field. But let’s face it, preparing for this comprehensive and challenging exam can be daunting. Whether you’re a seasoned professional or just… Read More »

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 »

img