Ansible Inventory Management
Introduction to Ansible Inventories
What is an Inventory?
An inventory in Ansible is a list of managed nodes (hosts) that you want to automate.
It defines which systems Ansible interacts with.
Inventories can be simple flat files or dynamically generated.
Inventory Basics:
Default location:
/etc/ansible/hosts
.You can create your own inventory file.
Formats: INI (default) or YAML.
Groups: Organize hosts into logical groups.
Host variables: Customize settings for individual hosts.
Group variables: Apply settings to entire groups.
Inventory Management
Creating an Inventory File (INI Format):
Define hosts and groups.
Example:
[webservers] webserver1 ansible_host=192.168.1.101 webserver2 ansible_host=192.168.1.102 [dbservers] dbserver1 ansible_host=192.168.1.201
Using Patterns to Select Hosts:
Patterns allow you to target specific hosts or groups.
Examples:
$ ansible webservers -m ping
(Ping all webservers)$ ansible dbservers -a "uptime"
(Check uptime on DB servers)
Dynamic Inventories:
Pull inventory dynamically (e.g., from cloud providers).
Use dynamic inventory plugins.
Example: AWS EC2 instances as inventory.
Organizing Inventory:
Create a directory with multiple inventory files.
Use different formats (YAML, INI, etc.).
Organize by environment, function, or location.
1. Using ansible-inventory
Command
ansible-inventory
CommandThe ansible-inventory
command allows you to view and manipulate your Ansible inventory. It provides information about hosts, groups, and variables defined in your inventory files.
To list all hosts and their details:
ansible-inventory -i inventory.ini --list
2. Host Variables
Host variables allow you to define specific settings for individual hosts. These variables override group variables for that particular host. Here is an example:
Suppose you have an inventory file (inventory.ini
) with the following content:
[webservers]
webserver1 ansible_host=192.168.1.101
webserver2 ansible_host=192.168.1.102
You can define host-specific variables in a separate file (e.g., host_vars/webserver1.yml
):
# host_vars/webserver1.yml
ntp_server: ntp.example.com
Now, webserver1
will use ntp.example.com
as its NTP server, while other hosts in the group won't be affected.
3. Group Variables
Group variables apply to all hosts within a specific group. For example, if all machines in the [webservers]
group use the same NTP server, set a group variable:
# group_vars/webservers.yml
ntp_server: ntp.example.com
This way, all hosts in the [webservers]
group will use the specified NTP server.
Use Cases
Host Variables:
Setting unique SSH keys for specific hosts.
Customizing configurations based on individual requirements (e.g., different database credentials).
Group Variables:
Defining common settings for all hosts within a group (e.g., NTP servers, DNS servers).
Applying security policies consistently across a group of servers.
Reference
Last updated