Managing your Ansible Inventory
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lesson, you will learn about managing your Ansible Inventory. By the end of this lesson, you should understand what an Ansible inventory is, how to define it, and the significance of organizing hosts and groups for efficient automation tasks.
This lesson will be completed on the ansible-controller
VM, so if you're not already logged into that VM, go ahead and do so now.
What is an Ansible Inventory?
An Ansible Inventory is a file (usually named inventory
) that lists all the nodes or hosts that are reachable by Ansible. This file is used by Ansible to communicate with the machines you want to automate tasks on. A typical Ansible setup involves multiple machines, and the inventory file is where you define these machines and organize them.
Defining a Custom Inventory File
To start, let's create custom project directory named code
, then we'll CD into the new directory:
# Create a custom project directory named code
mkdir ~/code
# CD into that directory
cd ~/code
Next we need to create a basic inventory file. This file can be in INI or YAML format. Here, we will use the INI format for simplicity. Create a file named inventory
in your working directory with the touch command:
# Creates a file named inventory in your home directory
touch ~/code/inventory
You will need to CD into the ~/code directory from now on when you run Ansible because this is where we are saving our config
Next, add either the following INI formatted or YAML formatted configuration to define our inventory:
[webservers]
managed-node-1
managed-node-2
[controllers]
ansible-controller
In this example, there are two groups: webservers
and controllers
. Each group contains the respective hosts. The group names can be anything that makes sense for your setup. Here is the equivalent inventory in YAML format:
webservers:
hosts:
managed-node-1:
managed-node-2:
controllers:
hosts:
ansible-controller:
Organizing Hosts and Groups
Organizing your hosts into groups is highly beneficial, especially when managing a large number of servers. Groups allow you to target a specific set of hosts for particular tasks. For instance, you might have tasks that should only run on web servers and others only on database servers.
You can also define variables for groups or individual hosts. These variables can be used to store specific data related to the hosts or groups, such as domain names, IP addresses, or any configuration specifics.
Running an Ansible Ping
Let's try using our inventory file to ping all hosts in the webservers
group. First, make sure you have Ansible installed and your hosts are reachable. Then, run the following command:
ansible webservers -i inventory -m ping
This command uses the ping
module to check the connection to all hosts in the webservers
group. The -i
option specifies the inventory file to use.
We can see the pong response coming back to our Ansible Controller, and that means we have connectivity to the managed nodes!
We can run this ping module against all hosts as well by running the following command:
ansible all -i inventory -m ping
This will run the ping module against all hosts in our inventory file:
Default Inventory File Location in Ansible
In addition to defining custom inventory files, it's important to understand the concept of the default inventory file in Ansible. This knowledge is essential for efficient and organized Ansible management.
What is the Default Inventory File?
Ansible automatically looks for an inventory file at certain default locations. The most common default location is /etc/ansible/hosts
. This file is used by Ansible if no other inventory is specified in the command line or in the Ansible configuration file.
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.