Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Ansible Inventory Tutorial

Introduction

Ansible is a powerful automation tool used to manage configurations, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates. One of the core components of Ansible is its inventory, which defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate.

What is Ansible Inventory?

Ansible inventory is a collection of hosts against which Ansible plays and tasks are run. The inventory is defined in a simple text file with a specific format. The default location for the inventory file is /etc/ansible/hosts, but you can specify a different inventory file using the -i option on the command line.

Basic Inventory File Structure

A basic Ansible inventory file looks like this:

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

In this example, there are two groups of hosts: webservers and dbservers. Each group contains a list of hostnames or IP addresses.

Defining Variables in Inventory

You can define variables for each host or group of hosts in the inventory file. For example:

[webservers]
web1.example.com ansible_user=admin ansible_port=2222
web2.example.com ansible_user=admin ansible_port=2222

[dbservers]
db1.example.com ansible_user=admin ansible_port=2222
db2.example.com ansible_user=admin ansible_port=2222

In this example, the ansible_user and ansible_port variables are set for each host.

Dynamic Inventory

In addition to static inventory files, Ansible supports dynamic inventory. Dynamic inventory allows you to pull host information from external sources such as cloud providers, CMDBs, and other inventory systems. To use a dynamic inventory, you need to provide an executable file that returns JSON output.

Example of dynamic inventory script output:

{
  "webservers": {
    "hosts": ["web1.example.com", "web2.example.com"]
  },
  "dbservers": {
    "hosts": ["db1.example.com", "db2.example.com"]
  }
}

Save your dynamic inventory script and make it executable. Then use it with the -i option:

ansible-playbook -i dynamic_inventory_script.py my_playbook.yml

Inventory Plugins

Ansible also provides inventory plugins that allow you to connect to various data sources to build your inventory dynamically. For example, you can use the AWS EC2 plugin to automatically fetch your EC2 instances.

Example configuration for AWS EC2 inventory plugin:

# ec2.yaml
plugin: aws_ec2
regions:
  - us-east-1
  - us-west-2
filters:
  instance-state-name: running
keyed_groups:
  - key: tags.Name
    prefix: tag

To use the EC2 inventory plugin, specify the configuration file:

ansible-inventory -i ec2.yaml --graph

Conclusion

In this tutorial, we've covered the basics of Ansible inventory, including static and dynamic inventories, as well as inventory plugins. Understanding how to manage your inventory is crucial for effectively using Ansible to automate your IT infrastructure.