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

  • By
  • January 19, 2023
0 Comment

16. Understanding Mixed Variable(Dict+List) with Playbook example

Hello. Welcome to this lecture. In this lecture we will discuss about complex variable consisting of board, dictionary and list variables. Here we have words section defined variable. Name is users. So this user’s variable is list of two items which are further dictionaries. So here this is first list item. And this is second list item. Both items are dictionaries. So here again we can use or we have to use Ginger. Two expressions to reference variable in player book. For example to display users variable. We’ll use this syntax to display first item in the list will use this. We know about this. So this will display this dictionary. Because first item is dictionary item.

Then we can move one step further to display name. In this dictionary we’ll use dot. Then we’ll use name as key to display this name. So in this way we can move one step further to reach the final value we are looking for. Similarly we can display a’s of Lisa by using item number one. And here using age instead of name as key. Now let’s move to the system and understand how this works. We are on ansible control node. And I am logged in as root.

Again I will open same playbook example in a dating mode. And we’ll make corresponding changes. Here variable name is users. And here we need to define two items. Each being dictionary name mark h 35. 2nd item is details about Lisa. Name Lisa is 30. I will delete these lines. And here we need to make corresponding changes. We’ll display complete variable users. So this is all we need to do. I will save and quit.

Now we know we need to execute this playbook. So here we have output. See this output. So we have scar bracket enclosing this variable. So this mean. So this is list variable which contains two dictionary items. Which is indicated by these curly brackets. So now to refer first item in this variable we’ll use zero. I will open this. And here inside scare brackets will mention zero to print first item of this variable which is this dictionary details about Mark. Again I will execute this playbook. Now see here we have dictionary in the output. Because first item is dictionary. Which is indicated by this curly brackets. Now to move further to display for example name of Mark.

We’ll use name key. I will open this. We need to move one step further in the dictionary. We need to print name. We’ll use name key. Here. I’m using Dot notation. We can use bracket notation as well. I will execute playbook. Again we have name in the output. Again I will open this playbook in editing mode. To display a’s of Lisa.

We’ll change this to one which is item one. And here we’ll use age as key. We’ll execute this playbook. We have 30 which is a’s of Lisa. So in this way we can access different values in different variables. You must be able to form different expressions to access variables because this is very important in case you need to execute some playbook based on some variable value. This is all about this lecture?

17. Using YAML file to Define Variables

Hello, welcome to this lecture. In this lecture we’ll discuss how we can use ansible variables defined in YAML file. In previous lecture we discussed example of list variable where each list item was further dictionary variable in the form of key value pairs. Where I would say this was mixed variable variable example here I’m using same example. But instead of defining variable in VAR section, I’m using file defining this variable. Here one file with name VARs underscore file YML is included and these are contents of this file.

This is the same variable we discussed in previous example. The only difference is instead of defining variable directly in the playbook, we are putting this variable in some YAML file. Then we are including that file using VARs underscore files section. So this is good way to define variables when you need to include or define multiple variables in some playbook. So, instead of putting all the variables in the VAR section, you can define variable in some YAML files or in the YAML file. Then using VAR underscore files section, list all the files to be used in the playbook. Now to access or to refer this variable, we can use exactly same expression we used in last example. So there will be no change at all. Now, let’s move to the NSIBL control node and understand how this works. Now we are on an ensable control node and I am logged in as root user.

First of all we’ll create file. To define our variable, we know we need to use variable file name as VARs underscore file dot YML as for example we discussed earlier. Here we’ll define our variable three dashes on the top variable name is users. Here we need to define two list items which are further dictionary variables. We have done three dots indicating end of YAML document. Now we’ll understand how we can use this variable file in playbook.

I will open same playbook in a dating mode. And here instead of VARs we must use VARs underscore files. Then here we’ll list the file defining our variable. File name is VAS underscore file YML. I will remove all this text. Now here I will make corresponding change. However, it will also work fine. First I will display complete variable. I will execute this playbook. So here we have list variable in the output where each list item is for the dictionary. So in this way we can list variables in some YAML file and include YAML file using VARs underscore files section instead of VARs section. In previous lecture we already understood how we can refer or access different list items. And further how we can refer to values associated with different keys. I’m not going to repeat this here. This is all about this task.

18. Setting Variables during Playbook Execution

Hello, welcome to this lecture. In this lecture we’ll learn how we can provide variable or variables during playbook execution. Here we must use VARs underscore prompt section. And here we’ll list our variables or variable as list item. Using name will define variable name. I am using using variable name as VAR. Using prompt, you can type text you want to display to ask for variable during playbook execution, I’m using enter variable private is set to know. This will be more clear when we’ll display this variable using playbook example again, to refer or access this variable, we’ll use same Ginger to expression. Now let’s move to the ensable control node and understand how this works. Now we are on ansible control node and I am logged in as root user.

Again, we’ll open same playbook and we’ll modify this playbook to define our variable. Here we know we must use VARs underscore prompt. Here we’ll use name directive to define name of variable which is VAR as per example. Then here we must use prompt to type text you want to display asking variable. I will type enter variable. I will remove this. And here I will change this to VAR. Because in this case variable name is VAR. Here I’m not using private directive just to show you which is the default behavior when we don’t use this. I will save and quit. I will execute this player book. Here it’s asking to enter variable. I will enter variable as ansible. You must have noticed you did not see this variable. So this is useful when you need to type some password.

So, default behavior is private is set to yes. I will press enter. So here we have variable displayed. Now I will again open this player book.

And I will set private to no. I will save and quit. We’ll execute this player book. This time we can see variable on the screen. So here we have variable displayed. Now we’ll discuss one practical example of creating user using VARs underscore prompt section. So that during playbook execution it will prompt to ask for username. We’ll open same playbook in the dating mode. And here I will change variable name to username. And here I will change it to enter username. Private is set to no. This is okay, because username is not critical to display. Then in the task section I will change this to create user. And here we’ll use user module. We already discussed about user module while discussing ansible ad hoc commands. But here we are using same module in playbook.

Here we’ll use named directive to define username. We must set name directive to this Ginger to expression state must be set to present. Although this is deferred behavior, I will include this. So, this is all we need to do. Now I will save and create will execute this playbook again, here it’s prompting asking username we need to create. I will provide username as test user for example, so user has been created on M host one. So in similar way you can also include password variable but for password we must encrypt password before using it. We’ll discuss more on this later on. Idea here is to make you understand how we can provide ancillary variables during playbook execution. This is all about this lecture? Yeah.

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