EX294 Red Hat Certified Engineer RHCE – Exploring Core Components of Ansible Part 5

  • By
  • January 19, 2023
0 Comment

13. Understanding Simple Ansible Variable with playbook example

Hello, welcome to this lecture. In this lecture we’ll learn about ansible variables. We know ansible playbooks are written in YAML. YAML supports dictionary variables, list variables and complex variables using dictionaries and lists both. So in this lecture we’ll understand what our dictionary variables, what are list variables, how we can use them together to use complex variables. Valid variable names should start with letter and can contain only underscore. So now here we are going to discuss what are dictionary variable dictionary variable as name indicates maps, keys to values. We can use keys to look for different values. Here is one example of simple dictionary variable. Dictionary variable example in the VARs section we’ll define this variable in this format.

So this is key. By using this key you can print this value key colon space value we can use defined variable in playbooks using Ginger two templating system. We’ll discuss about Ginger two templating system in more details in separate section. But here we need to keep in mind we must know how we can reference variable using Ginger two expression. For example in this case if we need to print the value assigned to this key, we must use this format. So this is ginger two expression to print this value double curly brackets. Then here variable name ol enclosure double quotation marks. This is important to remember at this moment. Now we’ll understand how we can define dictionary variable and how we can display dictionary variable using debug module. With the help of simple playbook we still need to discuss more about Ginger two expressions for conditionals in separate section which is very important topic from exam point of view.

For now, let’s move to the system and understand how we can use dictionary variable. Now we are on ansible control node and I am logged in as root. Here we’ll write simple playbook to understand how we can define dictionary variable and how we can print dictionary variable using debug module. Before we write playbook, I will display information about debug module. For this we know we can use ansible dash dock then module name. We’ll use MSG option to print our variable. So by default it will print hello word with sign of exclamation in the end.

So this is all we need to know at this moment. You can just see some examples here. Here we are using debug module. Then by using MSG option we are printing something again. Here we have Ginger two expression. Now I will create this. Now we’ll write simple playbook. I will name playbook as VAS extension must be dot YML or dot YAML. So make sure we are using this extension. This is very important. I forget to mention this earlier. Here I will change insert mode three dashes on the top enter space representing list. Here space in front of dash is note mandatory. Normally I don’t use this. In case we use this, we have to align playbook accordingly space. And here host colon space will give target as m host one we can give any target.

Because we are not going to do any action on the remote system. We just need to print variable. Now we’ll define words section wars colon enter so it will automatically indented to write. You don’t need to do anything but it will happen in case of you are using vim editor. If you are using via editor then you must take care of indentation level. Here I will define key colon value. I’m using same values as I showed you an example. So now we have defined variable. Here we need to define tasks section tasks space name colon here we can give some description displaying variable. Now we’ll use debug module and by using MSG will print this variable. We know ginger two expression to print this variable or to display this variable. We can print dictionary variable with the help of key.

So this is all we need to do. Now in dand three doors this is all we need to do. We’ll save and quit. And here I will introduce one more command line ansible playbook our playbook name is wars YML. So this playbook is present in home directory of root user. I will use dash dash syntax check to verify if everything is fine. So this is again important thing to know in the exam you can verify syntax of playbook before you execute playbook. Now I will execute this playbook ancient playbook name this is all we need to do. So here we can see target host. Here this task is about gathering facts. Having already said facts are always gathered by default. Then here is task to display variable. So this is our target host. And this is value of variable which is value. So in this way we can reference different dictionary variables in a playbook. Here we are using debug module to display variable. But normally we will not be displaying variables. But we are using same expression to reference different variables in playbook. We’ll discuss about them later on. Now again I will open this file.

And here instead of key I will use username and username is mark. We need to make corresponding changes here. This is real world example which we need normally to create users. Again I will execute this playbook. Now we have variable value as mark. I will clear the screen. I will execute same playbook with time in front of this timespace. Then everything same. It will display how long it takes to execute play. So it took around 7 seconds. Now we’ll do one more thing. Here we’ll use gather underscore facts and we’ll set this to false. Because we don’t need facts for this playbook. Again I will execute same playbook. This time it took only 3 seconds. So we can disable gathering of facts in case they are not needed. So this is all about simple dictionary variable. Now we’ll understand how we can use named dictionary variable.

14. Understanding Named Dictionary Variable with Playbook example

Hello, welcome to this lecture. In this lecture we will discuss about named dictionary variable. We already discussed about simple dictionary variable. Here difference is only that we are using name for the dictionary variable. So here we have wars sections. In War section we have DICT. So this is name of dictionary DICT colon. Then under this dictionary we have two key value pairs. Key one colon space value one, key two colon space value two. Again, by using Ginger two expressions we can refer these variables in playbook. For example, we can use this Ginger two expression to display dictionary variable. So this will display complete variable including two key value pairs.

In case we need to print a specific value. For example value one, we must use key one to display this value. So in this case expression will change. To this we can use dot notation DICT dot key one so it will display value one. Again, we need to use same expression. So here we have the python notation to display same value. We can use either of them. Both will work perfectly fine. This is also called bracket notation. Now again, by using debug module we’ll understand how it works. Now let’s move to the system to understand this. Now we are on ancillary control mode and I am logged in as root. We’ll open same playbook in a dating mode and we’ll make changes in this playbook to understand name dictionary. Example, here in War section we’ll add name for the variable which is dictas as for example colon. Enter key one, value one, key two colon space value two space is important. I will delete this line.

Now here we need to make corresponding changes. First of all we’ll display complete dictionary variable. We’ll execute this player book. So here we have complete dictionary variable in the output. These curly brackets in Python represents dictionary scare brackets in Python represent list. We’ll discuss about them in next example.

So when you see these curly brackets, so it means you are dealing with dictionary. Now again I will open same file or same playbook in editing mode. Now we’ll move one step further. Now we need to display value one. For this we’ll use key one. This is all we need to do. This is dot notation to display value. Again we’ll execute playbook. So here we have output value one. Again I will edit this. And here we’ll try bracket notation. Make sure you are typing correct syntax. Here again we’ll execute playbook. Again we have the same results as expected. So everything is working perfectly fine. In similar way you can display value two using key two. So this is how we can access or display different variables. I know we will know display variables during the exam. But of course we will be using variables as reference in different playbooks. And for that we must be able to form expressions to access or to refer variables. I am displaying these variables just to make you understand how we can make expressions to refer different variables. This is all about this lecture. In next lecture, we’ll learn about list variables.

15. Understanding Named List Variable with Playbook example

Hello, welcome to this lecture. In this lecture we will discuss about named list variable. So here we have VARs section defined VARs. Then here list. This is name of variable having three list items. So, this sign descend in YAML means list item. So we have three list items. Again, we can use ginger two expressions. Or we have to use ginger two expressions to reference these variables in a playbook. To display list variable, we can use this expression. We know how we can do this. Now, here one important difference between dictionary and list variable. Is that in case of dictionary variable we use key to display associated values. On the other hand, in case of list variable to print value of item, we use location of item and list. For example, first item zero location.

Second item, first location. This is same as we use in case of arrays. Here, using this expression we can print first item in the list. We are using zero enclosed in scar bracket. To refer this item. To refer this item, we’ll use one here. To refer this item, we’ll use two here. Now let’s move to the system and understand how this works. Now we are on ancient control node and I am logged in as root. Again, I will open same playbook in the dating mode. And we’ll make corresponding changes here. Here name of variable is List. We are using List variable.

In this case space item one, space item two space item three. I will remove these lines. Then the tasks section we need to make corresponding changes. Here we’ll use list. This is name of list variable. Now we’ll execute this playbook. Here we can see List variable is displayed. And all the items are enclosed inside scare brackets. So, these scar brackets means we are dealing with List variable. Don’t get confused with this. This is part of ansible output. Look at this output under MSG. So here we have three items enclosed inside scare brackets. So this means this is list of items. Now again, I will open the file in editing mode. Now we need to print first item.

We know what we can do. We’ll use scare bracket. Or we’ll enclose zero inside scare brackets to print first item. This is all we need to do. Again, we’ll execute this playbook. Now we have output item one. So this is what we were looking for in similar way. For example to list or to display item three. Now we have item three in the output. So, in this way we can use dictionary and list variables in playbook. And you should know how to deal with them, how to refer them in playbooks. Now, in the next example we’ll see combination of both dictionary and list variables.

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