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

  • By
  • January 19, 2023
0 Comment

19. Ansible facts and Accessing Ansible facts using Jinja2 expressions

Hello, welcome to this lecture. In this lecture we learn about ansible facts and how we can access different ansible facts variables. We know ansible facts are data information about remote systems like operating system, they use IP addresses assigned on different interfaces, hostname disk information, file systems and different types of information. We can access this data by using dictionary variable ansible underscore facts. By default we can also access some variables as top level variables starting with ansible underscore. Then here you will find variable name. I will show you when we’ll display ansible facts. We can display ansible facts using setup module using ansible ad hoc command we can use ansible. Then here we’ll provide host for which we want to display facts m to provide module name. So here one thing to keep in mind as normal users you will not be able to display all the information about remote system.

For example information about disks and the information which only root user is allowed to access. So make sure if you are executing this command as normal user you will miss some information you can use become flag with this command to display all the information about remote system. Now let’s move to the system and learn how we can display different ansible facts. Now we are on ansible control node and I am logged in as root. First of all I will display information about setup module using ansible dock. So here is description. This module is automatically called by playbooks to gather useful variables about remote systems that can be used in playbooks.

So we know by default when a playbook is executed it gathers facts about remote systems. We can use setup module to display facts about remote systems. Here we have different options to gather facts about remote systems but we are not using any of them right now we just use setup module without providing any arguments. Now I will quit, I will clear the screen, I will execute ad hoc command and symbol again I will use Mhost one stargetm to provide module name and I will press enter. Here you will find long list of ancible facts variables. So this is all information about m host one in similar way you can display information about different remote systems. So here on top level we can see ansible underscore facts dictionary variable contains all this variable.

So this variable is a dictionary variable. So this is clear from this curly bracket which further contains more dictionary items, list items and key value pairs. We must be able to access different types of variables. I will explain how we can do that. First of all in case if we need to print this IP address how we can do this? We know ansible fax is a dictionary with name ansible facts and this is one of the keys in this dictionary we know how to display value associated with this key which is further a list item. Now what we’ll do we’ll display value associated with this key. This is one more key under this dictionary. And in similar way we have many dictionaries under this top level dictionary which further contains more dictionaries and more list items. We’ll learn about them soon. Now I will clear the screen. Now again we’ll create one simple playbook. To understand how we can refer different ansible facts variable, I will write simple playbook with name facts. And we need to use extension dot YML. I will change to insert mode. And here every YAML file should start with three dashes on the top. And here then hosts we are using M, host one s Targate.

We don’t need to use become directive for privilege escalation because we are already using root user. In tasks section we’ll use debug module. I will provide some description. Here displaying facts variables. We’ll use debug module. And here using MSG, will display top level dictionary variable which is ansible underscore facts. And here don’t forget double quotation mark. This is all we need to do. In the end I will use or we need to use three doors. Now we’ll execute this playbook. Now we have this long output. So here one important thing I would like to tell you.

We did not provide or we did not set gather underscore facts to false. Because in this case if we would do that then we’ll get nothing in the output. We should allow playbook to gather facts so that it can display this variable again, I will show you if I do this gather underscore Facts and set this to false will again execute this playbook. We have nothing. So this is normal. So I just wanted to show you that in case you are using ansible facts variable in your playbook, you must use ansible underscore facts set to true which is by default. But if you will use this, it will note harm. Now again I will open this playbook and I will set this to true. I will execute this playbook again, it will take some time. Now here we will find all the information which we displayed earlier using setup module. So here we can see here we have ansible underscore facts dictionary variable value which further contains dictionaries and lists under this. So in case we need to display this IP, what we can do, so we need to use this key all underscore IPV, four underscore addresses. Now what we’ll do, we’ll open this file again. And here using dot notation I will type or underscore IPV four underscore addresses I will save and quit. Now we’ll execute this playbook again. So here we have output.

So we have this list item associated with this key. Now further if we need to display this IP, what we can do, we know this is list containing only one item. Now I will clear the screen again, I will open this. And here we’ll use scale brackets with zero inside to display first item in the list. We have only one. Now we have the IP. In this way we can refer to different facts variables. So this was simple one. Now what I will do, I will execute ad hook command to display fax variable or to access or to gather fax variables about m host one. And I will grab the output for ansible underscore. I wanted to show you something important here. So here you will find many simple dictionary variable which are key value pairs. For example, in case you need to display ansible node name, you can use this key ansible underscore node name which is mapped to this value. So this value will be displayed. We know we can use Ginger to expression to display value associated with this key.

So this is simple form of variables. You will find many variables in simple form of key value pairs. For example ansible underscore OS underscore family. So this is red hat. Similarly we have ansible underscore hostname. We have ansible underscore FQDN. So if you see this variable so this is node simple key value pair. So there is something below this. Because we only printed lines containing ansible underscore. So this is note shown. So to access variable under this, we need to form complete expression. Now again I will use same command. But here I will use a option to display ten lines after a match is found. To be more clear how they look like. So here these variables are simple key value pairs. But these variables are node. So these variables further contains list items here. So similarly you can just go through and check different variables.

So these are simple variables, these three. But this variable is dictionary variable further contains key value pairs. Now I will clear the screen. So here I will clear all this as I showed. You will display simple key value pairs unsieble underscore node name. I will save and quit. We’ll execute playbook again it will take few seconds. So we have the output. Because this is simple variable here, I would like to tell you one thing. You can access or refer to same variable using ansible underscore facts as dope level variable. Then use dot. But you must remove this ansible underscore. This should not come twice in expression. I will execute this playbook again we have same output again I will open this file in editing mode. Instead of dot notation, I would use python notation or bracket notation. You will find this notation used many times. Now again output is same.

So now this is very important in case you need to execute some playbook depending upon hostname or node name. For example if node name is amhost one dot example do this action. If node name is Mhost two example do that action. We’ll do these kind of tasks later on. Now I will clear the screen again. I will display facts by using setup module. Now we’ll learn how we can access complex variables. Or I would say how we can go further inside complex variables. For example, we need to display sizo partition one FDA one. How we can do that? Now we’ll just walk through this information. If you see we have size here 300 MAB. Now we will understand how we can form expression and access this variable. We’ll move on the top and here we can see ansible underscore devices having already set. We can access different facts starting with ansible underscore traffic. We’ll use this dictionary variable to display all the information under this. Then we’ll move further to reach to this key value. Now what we’ll do, we’ll open same file. And here I will clear everything.

And here I will use ansible underscore devices. So this is dictionary variable. We will save and quit. I will clear the screen. We’ll execute this playbook. Now we have all the information under this dictionary variable and siebel underscore devices. Now we need to move further. Now, here we have one more dictionary with name SDA. Now what I will do, we’ll move step by step. Here I will use SDA. I will save and quit. Again we’ll execute this playbook. Now we have more limited information. Now under this SDA we have more dictionaries and key value pairs. But we are interested in this dictionary which contains information about SDF one. Now we’ll move one step further and we’ll print or access partitions dictionary. Again I will add dot partitions. I will clear the screen. Sorry. We’ll execute this playbook again. Now we have this information. Now we need to move to this dictionary which further have this info. Dictionary name is SDA one. Again we’ll execute this playbook. Now we have more limited info. Now we feel we can reach this information. Now under this dictionary we need to access this key. We just need to use dot size. This is all we need to do. Here I will use dot size. I will clear the screen. We’ll execute playbook again. Now finally we have the size of partition SDA one which is 300 mabides.

In this way you must be able to form expressions to refer to different ancible facts variables. Because you are going to encounter such kind of tasks on the exam which involves different facts, variables and other variables. Now again I will open this file and instead of dot notation you will find bracket notation used most of the times. I will change this to bracket notation or python or python notation. Now it’s fine. Again we’ll execute playbook to verify. It’s still working fine. So it’s fine. I will make one last change. As I told you earlier, we can use ansible facts top level dictionary. Then further we can use devices. We should not use ansible underscore devices anymore. So this will work also fine. So same output. So I’m telling you this because you might find these kind of expression used most of the times. Starting with ansible underscore facts having Goldade said we will not would be displaying variables on the exam but definitely we will be using such kind of the expressions on the exam for conditional playbook execution.

20. Filtering Ansible Facts using gather_subset and filter

Hello, welcome to this lecture. In previous lecture we learned how we can access and display ansible facts of remote nodes using setup module, but we did not use any filtering options. In this lecture we will learn how we can display subset of facts instead of displaying all the facts. We will also learn how we can display ansible facts matching some pattern. To display subset of facts and to display facts matching some pattern, we can use gather underscore subset and filter parameters using e option with setup module. Here are examples of using gather underscore subset directive. We can use this example to gather facts related to network. I’m using dash dashbum to make sure all information is gathered. So when we execute this command, only facts related to network will be gathered along with minimum default facts which are always gathered when we gather any subset of facts. Then next example to gather hardware related facts here we need to set gather underscore subset to hardware rest everything is same. Again I’m using dash become to gather all information related to hardware. Again, it will also include minimum default subset of facts.

Together virtual environment related facts we can execute or we can use this sensible ad hoc command. Here I’m excluding minimum default facts by using sign of exclamation in front of men. So we can negate by using this sign here. You might have noticed that I used single quotation marks to enclose this argument instead of double quotation marks which are used here, I did this for a purpose. Because when you use sign of exclamation for negation purpose, you must enclose the argument in single quotation marks. If you will enclose this argument in double quotation marks, you will have some error. I’ll show you when we move to ensure that control node, this is something you must keep in mind. Default is all. We already executed this command without using dashu option. In that case, all facts will be displayed. Here we have one more example. To gather facts accept network related facts. Again, here we need to use sign of exclamation in front of network so that all facts will be gathered except network related facts.

Now here we have some example showing uses of filter directive. So in this first example, it will display all the facts under this dictionary variable ansible underscore devices. In previous lecture we refer to size of partition where we used this dictionary variable. But in case you know your fact variable is present under some particular dictionary variable, you can display information under that dictionary variable using filter directive. It will be more clear when we’ll execute this command on ensable control node and we’ll analyze the output. Here we have same example instead of nsible underscore devices dictionary variable, I used ansible underscore LVM dictionary variable. So this will display information about volume groups and logical volumes if created on your system.

Third example is showing how we can use filter to display unsafe facts matching some pattern. So this will display ancient facts starting with ancient underscore and ending with name. There can be anything in between these two expressions. I will say then in the last I mentioned two notes. When using this symbol to negate some value, we must use single quotation marks. Don’t use double quotation marks, otherwise it will fail. Then note two minimum default facts are always gathered with each subset of facts until we exclude them using negation symbol in front of men. Now let’s move to the ansible control node and start exploring this further.

Now we are on Ansible control node. First of all, I will display documentation for setup module an siebel dash tok setup we’ll discuss directives we are going to use in this lecture filter by using filter, we can filter facts matching some pattern. We can use wild card while specifying pattern, then gather underscore subset. We know we can use this directive to display or access facts related to specific subset. For example network, virtual, hardware, vector and there are more the next gather underscore timeout parameter we can use this to set default timeout in seconds for facts gathering by default value is ten. Then here in the last you will find examples how you can use filter and gather subset directive or I would say gather underscore subset.

According to these examples, all the facts related to network and virtual will be displayed along with minimum default facts. And here, as per this example, only minimum default facts will be displayed. Here, sign of exclamation is used in front of all. So it means exclude all the facts, but still it will gather minimum default facts to exclude them as well execute or use this command. Here you can see, you can mention multiple arguments or values separated by comma. So in this case nothing will be displayed. I will quit now, I will clear the screen.

Now we’ll display ensible facts of m host one. But this time we’ll use gather underscore subset and filter directives to filter facts nsibel mhstone m to specify module A to provide arguments here I will use gather underscore subset. I will set this to network become to display all the facts related to network. It can take some time. Here we have facts, here all the facts related to network including default minimum facts are displayed. Here you will find all the IP information and other network related info. For example, in case we need to display or access IP address assigned to ENP zero s three interface so we can easily find it out from the network related facts instead of displaying all the facts, then searching for variable we need so here we know to access IP address, we can use this dictionary variable. Under this dictionary variable we have IPV four. Then further, to display IP address, we can use address key. In similar way, we can access or refer to IPV six address, but here, this is list item.

So inside list we need to use this key to display this value. We know how we can form expression for this idea here is that we can display subset of facts instead of all the facts to make the life little easy. Next I will do one change. Here I will use coma sign of exclamation, then min. So this is done to exclude minimum default facts, I will press Enter. We have error event note found. I did this with intention just to show you, this will not work here we need to use single quotation marks. I will replace double quotation marks with single quotation marks. This time it will work. So now we have more limited output. So only facts related to network are displayed. I will clear the screen. In similar way, we can display hardware related facts by using hardware instead of network, it will display all the facts related to hardware. For example, disk info which we accessed.

In the last example or lecture, we displayed size of SD one partition. So here we can easily see this is a dictionary variable which we used in last lecture. Under ansible underscore devices we have SDA next level dictionary. Under this we have partitions. Then we have sta one dictionary. Under this we used or we can use size key to access or to refer to this size. So in this way, by limiting the unsuble facts, we can easily find out different variables. So in case we need to display facts related to virtual environment, we just need to replace hardware with virtual. Now facts related to virtual environment will be displayed. We have only these facts. Now this is all about gather underscore subset directive. Now again I will display ansible facts of m host one. But this time we’ll use filter directive. So I’m using filter set to an Siebel underscore devices. So it will display all the facts under this dictionary variable. So here we have output. Here is our dictionary variable ansible underscore devices. Under this we have SDA which is our hard disk. Then we have partitions different partitions SDA one, SDA two, SDA three.
So in this way we can use filter to display facts under some dictionary variable. But we must know dictionary variable name to display these facts. I will clear the screen. Here I will use LVM. We don’t have anything in the output because we don’t have any logical volumes created. We don’t have any physical volumes, we don’t have any volume groups. So all three dictionaries are empty. So when we create logical volumes volume groups, you will find data under these dictionary variables. Now, next I will use wild card. So it will display all the variables starting with an Siebel underscore and ending with name. So these variable can be simple key value pairs or complete dictionary variable.

So here we have simple key value pairs. Now I will make one more change. Here I will use family instead of name. So here we have an Siebel underscore OS family again I will change or replace family with devices to see what we see in the output. So here we have NCAL underscore devices variable displayed so it means by using filter directive with some pattern we can display all the variables matching pattern. So variables can be simple key value variables or complete dictionary variable containing further information under this so in this way we can filter and simple facts variable. So I will suggest to practice more how you can use filter and gather underscore subject directives. This can make your life easy on the exam otherwise you will need to display all the facts then search for the variable you need. This is all.

21. Getting Help- Ansible Documentation

Hello, welcome to this lecture. In this lecture we will learn how we can find ansible documentation. Ansible documentation is available on website docs. Ansible. com. We know by using ansible Doc command line tool we can display information about different modules, tools, various directives that can be used and some playbook examples. We already gone through this, so here I specified one example to display info about copy module to display all modules available execute ansible tok l now we’ll navigate to this website and we’ll explore information contained here. When you open this URL docs tensible. com in browser you will land on this page. Here you can find ansible documentation. Click here. Here we need to change the version to two eight which is exam version.

So here you can find all the information about Ansible starting with Introduction to Ansible then how we can install ansible installation guide then how we can use Ansible working with command line tools introduction to Ad hoc commands working with inventory working with dynamic inventory this is not part of this exam. You can skip this. Working with Playbooks understanding privilege escalation ansible Vault which we still need to discuss. Working with patterns and many more information. You can further click on topic to have more information on the topic. Here you will find all the information how to work with playbooks about Playbook playbook language example and many more information here. Using variables creating valid variable names defining variables in inventory defining variables in playbook how to use Ginger two with variables so in this way you can find all the information here I will suggest to go through this documentation while preparing for the exam. This would be very helpful. This is all about this lecture.

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