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

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