🚀 Welcome, Pythoholic tribe! Learn about the fundamentals of the Ansible inventory, a key component for effective automation and configuration management. This video serves as an Ansible tutorial for beginners, explaining how the Ansible inventory works within the broader devops landscape. Find out more about this important topic!
Chapters: 00:00 Intro 00:19 Understanding Ansible Inventory 03:45 Example Inventory Show 05:23 YAML Inventory 07:00 INI Inventory 09:30 MetaGroups in Inventory 10:38 Creating Variable for Inventory 12:22 Organize Inventory in Directory 16:05 Using Dynamic Inventory
Support our mission: 💙 INSTAMOJO: https://instamojo.com/@pythoholic 💙 PAYPAL: paypal.me/pythoholic 💙 Check out the tech I use and recommend: www.amazon.in/shop/pythoholic
Connect and follow for more learning resources: 🔔 Podcast (Blabber Cloud): http://tinyurl.com/Pythoholic 🔔 YouTube: tinyurl.com/PythoholicYT
🚨Disclaimer: The content provided on this channel is independent and not affiliated with any organization. Our videos are based on our knowledge and understanding of the topic, and we recommend viewers conduct their own research to gain a fuller understanding. We're always seeking to improve, and your feedback helps us do that! Support the channel for more content like this in the future.
Hey everyone, before we dive into today's Ansible session, I have something exciting to share.
I am launching a complete mastering Ansible for beginners course very soon and there's a link in
the description to join my waiting list and you will get early access, exclusive discounts and
be notified the moment it goes live. Now let's understand the Ansible's inventory.
Now let's take this example once again to understand Ansible.
So here the Ansible master control node is the command center. From here the commands are sent
to our various targets in our network. It is basically the hub from which all our automation
tasks are coordinated. Now look at these targets. So each represents a server or group of servers
where our applications and services reside. Notice how they have grouped under two categories
or how we have grouped them into two categories. Let's call them project Netflix and project
YouTube. These groups help us organize our servers by their roles or the services they run. So the
line between the control node and each target node represents the communication channels.
So Ansible uses SSH, a secure protocol to talk to each target, issuing commands and rolling out
changes. And target A and B belong to project Netflix. Here Ansible might be deploying a new
streaming service or updating an existing one. So it's all about ensuring that the viewer's
experience is seamless and enjoyable. And on the other hand, we have target C and target F,
part of the project YouTube. So these servers could be getting updates to enhance video
processing or to scale up the infrastructure to meet high user demand. And you as a developer
can update servers just using the connection mechanism to connect to the target servers by
using Ansible. But Ansible control node should know what are these servers and the way they are
defined so it can execute specific operations on these target servers. And that's where we need
the help of Ansible's inventory. So this is an inventory file. Within its syntax lies the
framework of our commands. You can call it a registry of targets that Ansible will connect
and execute operations under its wings. So this inventory.ini file is a roster,
the starting block of our automation. So as you see here, it is sectioned into groups like YouTube
and Netflix, each with its group of servers. So I want you to notice the structure here.
So each server, a line item defined by its IP address, ensuring Ansible knows precisely who
to call. And we secure our conversations with SSH. And also it can be done by password and private
key files as well, which we can secure it on our secure vaults, which we will discuss later on in
this course. And there at the bottom, you can see a group for streaming apps. It is a simple list,
but in Ansible's language, it's called groups. So within the group streaming apps, we have YouTube
and Netflix under one banner for tasks that apply to all the streaming services. So if you want to
execute operations individually on YouTube or Netflix servers, you can do that by executing
operations on the tags that you have YouTube and Netflix respectively. And for the operation that
needs to put across all servers, you can use the group as well that is called streaming apps.
So this is a basic idea of how our inventory file should look like. The inventory file is like our
address book and Ansible uses it to connect with the servers we are automating. So let's start with
the inventory.ini file. So it's the starting point where we define groups of servers and their
details. To ensure everything in place, we'll run a simple command ansible-youtube-m
ping-i inventory.ini. So here, as I already told you, I want to ping the group of servers
that are mentioned within the tag YouTube. So if I do this, and if I run this command, all the
posts that are belonging to that particular YouTube tag or group will get executed. Now,
what I'm going to execute, I'm just going to execute the ping command. So it'll ping
each of these servers that are listed within YouTube. So you can see here, it looks like
the video server 1 and video server 2 have responded with the pong, which is Ansible's
way of saying all is good. But notice server 3, the video server 3 that you see here,
it is unreachable. So this is a crucial part of our check. It tells us where we might have issues
that need troubleshooting. So by running these commands, we keep our automation process
transparent and under control. And remember one thing that is really important. So regular checks
like these are vital for smooth automation workflows. So they ensure that when we want to run
our playbooks, they execute flawlessly, keeping our application running without a problem.
And Ansible's inventory can be defined in both YAML and INI formats. So inventory file is a place
where we list the nodes or hosts along with their IP addresses or host names that we want to manage
using Ansible. So this is something that we all know by now. So I've taken the same inventory
example on the left-hand side that you see. In the YAML format, we have created a structure hierarchy
where all is the top-level group that contains children, which has subgroups like YouTube. Under
YouTube, we have hosts where each host is a key. For example, video server 1, video server 2,
and video server 3. Under which we have the host name, username, connection type, and the password.
This is basically a YAML format. And on the right-hand side, this is the INI format. We simply
list the group name in the square brackets and then list each host in that group line by line
underneath. This is the same way we have been seeing all along. So you might ask me,
what is the use case of both? Or is there a condition where we should make use of either?
So my advice would be to choose the format that best fits your project or your preference or
readability and what actually is being used in your organization. So YAML is more readable or
human-readable and better for complex configurations, while INI is simpler and may
be preferred for smaller or straightforward inventories. Let's look at our inventory INI
file here. So you will see groups labeled YouTube and Netflix, each with a list of member servers.
So these groups actually represent type of streaming servers. Moving to the network group
that you see here. Notice the children keyword here. This is a way we can organize things in
Ansible. So by defining YouTube and Netflix as children, we are telling Ansible that these are
subsets of the network group. Next, observe the web server group. So it's a group that contains
web servers. Next, this brings us to the data center metagroup. So here we combine network
and web servers into a single overarching group. So it's a way to manage these related resources
together, making automation tasks more efficient. So here you might ask me, how are groups and
metagroups different? So I call web servers as groups and data centers as metagroups. So listen
to this very carefully. So a group refers to a collection of hosts that you manage together.
A group allows you to apply configuration changes or deploy software to many servers at once rather
than one at a time. For example, we have a group called YouTube, Netflix, or web servers. So these
are groups. Now a metagroup, on the other hand, is essentially a group of groups. So it allows
you to organize multiple groups under a single umbrella for even broader orchestrations. For
instance, if you have a separate group for networks or web servers, you might combine them
all into a metagroup called data center, as I've shown here, to represent all servers related to
your data center environment. Similarly, we have a metagroup called IT infrastructure,
which includes YouTube and Netflix as its children. So you must understand that a group is
a collection of hosts and a metagroup is a group of groups. And there are ways to define it. So
one way is the way we have defined the network metagroup with the children key here under the
tag. And the other way is using the children indicated as a suffix in the name for the metagroup
IT infrastructure. So in both cases, for both network and IT infra, and this is useful when
you want to apply common configurations or commands to multiple groups at once. But here, I want to
tell you that you should use a standard way throughout your development cycle. So I would
recommend it to be the way we have defined IT infrastructure colon children. So we have already
seen the inventory file, or we have already seen this inventory file. But you might be thinking,
how would we execute the metagroup? So here it is. So as you can see, we have executed the command
With a simple ansible command, we ping all hosts. So here you can see we have two servers from
YouTube. There is video server one, video server two, and two servers from Netflix,
movie server one and movie server two, which are a part of the IT infrastructure metagroup.
And we see that other groups or hosts don't get executed. Only the one that we used,
IT infrastructure, which has the YouTube and Netflix as the children, only those servers get
executed. And I did that by writing the command ansible space, the name of the metagroup hyphen
M space, the module name that is ping hyphen I, the inventory file name. Now let's talk about
creating variables for our inventory. We are looking at how to make our ansible inventory
more dynamic and manageable. Sorry. Now we are looking at how to make our ansible inventory
more dynamic and manageable by creating variables. So variables are like shortcuts or alias that
store data we frequently use such as IP addresses, system settings, and user credentials. So let's
examine our inventory.yaml file here. So here you can notice the group streaming service or
streaming service. So it's our container for servers we are focused on. So within the group,
we have two hosts, YouTube server and Netflix server. So each host has an associated IP address
and a unique variable that tells us the streaming service that provides the streaming service
YouTube streaming service Netflix. Now let's talk about the variables. So this section actually
defines variables that are common to all the hosts within the group. For our servers, the ansible
user that is admin, which is set to admin, which means that we'll connect to both servers using
this username that is admin. We have another variable here is server location set to data
center one. So this is a crucial piece of information indicating where our servers
physically reside or which data center cluster they belong to. So by defining these variables
in our inventory, we can run ansible commands more efficiently. And I would want to tell you
one thing that as you create your ansible playbooks, remember to leverage variables to make
the automation tasks more flexible and easier to maintain. It's all about writing less and doing
more. Now let's talk about how we can organize our inventory in a directory. Now let's talk about
how we can organize our inventory in a directory. So ansible actually allows you to consolidate
multiple inventory sources in a single directory, offering a simpler version compared to a single
complicated inventory file. So let's break down the inventory directory that we have here.
So inside we find a variety of files, aws-ec2-yaml for the dynamic inventory for aws-ec2,
static-inventory.ini for fixed hosts and groups, and dynamic-inventory.json for more fluid
infrastructures. These formats offer flexibility in how we define and interact with our servers.
Now let's look at them by opening each of them and understanding what they actually can help us
with. To take a closer look at aws-ec2.yaml, so it is configured to pull in instances from a specific
AWS region with a certain tag. So this dynamic inventory adjusts in real world as our cloud
infrastructure changes. So why we call this as dynamic inventory is because we don't have the
list of hosts which are a part of the instance fleet on our AWS EC2 environment. So this dynamic
inventory helps us to pull the instances in the region us-east-1 which have the tag my-web-server.
Alongside the dynamic inventory, we maintain a static-inventory.ini file. The ini format here
is straightforward showing the groups like web servers and database servers. So with each group
containing respective hosts like web-01, db-01. This file is ideal for static environments where
the infrastructure doesn't change frequently. So we have a static host and groups. So which
actually we can put in all the host names that we want and this will be static. So it does not
change. Next, we also have a dynamic-inventory.json which is our dynamic inventory in the JSON format.
So this format is versatile and can be generated by external sources or scripts
providing real-time data about our managed hosts. So it has dynamic-host-1, dynamic-host-2
and it has IP addresses and the Ansible user. So this could be generated externally as well
and using script and provided which actually provides real-time data about our managed hosts.
So the group variables that you see here. So the group variables organized under the
group-vars directory. Here we find web-server.yaml and database-server.yaml.
Which hold variables specific to our web and database servers respectively. So this setup
enables us to assign variables that are automatically applied to the hosts within
those groups. And when we want to execute a playbook, we can just do that by executing a
command which is ansible-playbook playbook name that is playbook.yaml-i the inventory directory.
So this command tells Ansible to use all inventory sources located within our inventory directory.
So Ansible automatically combines these sources, these sources, all the sources that we have here.
Ansible automatically combines all these sources utilizing the static dynamic
inventories along with the group variables to create a consolidated inventory for the playbook
run. The structure actually shows the importance and the flexibility and the power of Ansible's
So by using a combination of static dynamic and group specific variable files,
we can tailor our inventory to match the exact needs of our environment.
Ensuring that our automation is as efficient and effective as possible.
Let's talk about dynamic inventory. This is also very important.
Okay, so let's talk about dynamic inventory a bit more so that we can understand it better.
So unlike the static list we are accustomed to, dynamic inventory in Ansible is a live construct,
an evolving entity. So it's not just a file, it's a gateway to real-time cloud state awareness.
Why am I saying this? Because it is. Because you get the real-time data about the cloud state.
So it uses scripts or plugins to query external systems such as AWS, GCP or VMware. Take a look
at the left-hand side of the screen. So AWS EC2 YAML file is not just a YAML, it's a lens into the
AWS EC2 services. So it lists regions, identifies instances with a tag and organizes them into
groups. Moving on to the right-hand side, you can see we have the operation with the command
Ansible playbook. We invoke the Ansible EC2 inventory plugin and Ansible reaches out and
fetches the live state of our cloud environment. So here Ansible will dynamically query AWS EC2
instances in the specified region and use them as the inventory. And below we have an example output
considering a playbook where we install Apache, start services and do so across a fleet of EC2
instances that didn't need to be manually listed. So they were discovered, targeted and managed
dynamically. So that is why dynamic inventory is so, so, so, so important. And we were able to do
this just by executing the command Ansible playbook hyphen I AWS EC2.YAML space the playbook.
And we could perform the operation using the inventory file AWS EC2.YAML and the operations
defined in the playbook. So I have not given the playbook exact playbook here, but just think about
the AWS EC2.YAML, which has this plugin AWS EC2, but we will be using the fully qualified
collection name. Don't worry about it. We are just writing AWS EC2 here for now. And this operation,
once we execute it, it performs the operation using the inventory file and the operations
defined in the playbook. And here, these instances that you can see in the output,
when not manually added into the inventory file, we use the Ansible plugin Amazon.AWS.AWS.EC2
to fetch the details directly from AWS. Similarly, we have for GCP and Azure as well.
So if you're using other cloud services, you are sorted out on that part as well.