ansible inventory

‘Chapter 4. Inventory: Describing Your Servers’
—Bas Meijer, Lorin Hochstein, Rene Moser, “Ansible: Up and Running”

Core Idea

The inventory is Ansible’s map of hosts: static or dynamic, grouped with variables, and extensible through inventory plugins that pull machines from Docker, cloud, or elsewhere.

  • Behavioral inventory parameters and groups (basic with group vars, nested, numbered).
  • Dynamic inventory and inventory plugins, including common built-ins and a Docker example.
  • Directory-based group variables and adding entries at runtime.

The collection of hosts that Ansible knows about is called the inventory.

Behavioral inventory parameters

In Ansible, behavioral inventory parameters are configuration options that determine how Ansible interacts with hosts in the inventory. These parameters control aspects such as connection methods, user privileges, and specific runtime settings.

NameDefaultDescription
ansible_hostName of hostHostname or IP address to SSH to
ansible_port22Port to SSH to
ansible_user$USERUser to SSH as
ansible_password(None)Password to use for SSH authentication
ansible_connectionsmartHow Ansible will connect to host (see the following section)
ansible_ssh_private_key_file(None)SSH private key to use for SSH authentication
ansible_shell_typeshShell to use for commands (see the following section)
ansible_python_interpreter/usr/bin/pythonPython interpreter on host (see the following section)
ansible_*_interpreter(None)Like ansible_python_interpreter for other languages (see the following section)

You can override some of the behavioral parameter default values in the inventory file, or you can override them in the defaults section of the ansible.cfg file.

Groups

Basic Groups and Group Vars

[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
 
[databases]
db1 ansible_host=192.168.1.20
db2 ansible_host=192.168.1.21
 
[webservers:vars]
ansible_user=ubuntu
ansible_become=true
 
[databases:vars]
ansible_user=postgres
ansible_become=true
ansible_python_interpreter=/usr/bin/python3

Nested Groups

[webservers]
web1
web2
 
[databases]
db1
db2
 
[production:children]
webservers
databases
 

Numbered Hosts

[web] 
web[1:20].example.com
[web] 
web-[a:t].example.com

Dynamic Inventory

Dynamic Inventory is a feature in Ansible that enables automatic fetching of host information from external sources, such as cloud platforms, containers, or custom APIs.
(This approach eliminates the need to maintain a static inventory file, making it ideal for environments where hosts are frequently added, removed, or modified.)

If the inventory file is marked executable, Ansible will assume it is a dynamic inventory script and will execute the file instead of reading it.

Inventory Plugins

Ansible inventory plugins are components that allow you to dynamically define and manage inventories in various formats and sources.

  1. Built-in Inventory Plugins: Ansible comes with several built-in inventory plugins that support common use cases, such as fetching inventory data from files, cloud providers, or APIs.
  2. Custom Inventory Plugins: Users can develop custom plugins to meet specific requirements or integrate with proprietary systems.

To see the list of available plug-ins:

$ ansible-doc -t inventory -l

Common Built-in Inventory Plugins

Plugin NameDescription
iniParses inventory from ini-formatted files (default inventory format).
yamlParses inventory from YAML files.
host_listReads inventory from a list of hosts provided inline.
scriptExecutes a custom script that generates inventory dynamically.
directoryLoads inventory from multiple files in a directory.
cloudRetrieves inventory from cloud providers (e.g., AWS, GCP, Azure, etc.).
constructedCreates a dynamic inventory by filtering or transforming data from another inventory source.
openstackFetches inventory from OpenStack.
azure_rmRetrieves inventory from Azure Resource Manager.
aws_ec2Gathers inventory from AWS EC2 instances.
gcp_computeFetches inventory from Google Cloud Platform Compute Engine.
vmware_vmRetrieves inventory from VMware environments.

Docker Dynamic Inventory

ansible-galaxy collection install community.docker
plugin: community.docker.docker
compose: yes
containers:
  - name: app_container
ansible-inventory -i docker_inventory.yml --list

Directory-Based Group Variables

Ansible handles inventory management when using a combination of static inventory files and dynamic inventory scripts.

ansible.cfg

[defaults]
inventory = ./inventory
ansible-playbook -i ./inventory playbook.yml
inventory/
├── aws_ec2.yml        # Dynamic inventory for AWS EC2
├── azure_rm.yml       # Dynamic inventory for Azure Resource Manager
├── group_vars/        # Variables specific to groups
│   ├── vagrant        # Variables for Vagrant hosts
│   ├── staging        # Variables for staging environment
│   ├── production     # Variables for production environment
├── host_vars/         # Variables specific to individual hosts
│   ├── host1          # Variables for "host1"
│   ├── host2          # Variables for "host2"
├── hosts              # Static inventory for generic hosts
├── vagrant.py         # Dynamic inventory script for Vagrant
# inventory/host_vars/webserver1
ansible_host: 192.168.1.100
ansible_user: ubuntu
app_environment: production
app_port: 8080

Adding Entries at Runtime

add_host

The add_host module adds a host to the inventory; this is useful if you’re using Ansible to provision new virtual machine instances inside an infrastructure-as-a-service cloud.

---
- name: Dynamically add a host to inventory
  hosts: localhost
  tasks:
    - name: Provision a new VM (simulated)
      command: echo "192.168.56.101"
      register: new_vm_ip
 
    - name: Add the new VM to the inventory
      ansible.builtin.add_host:
        name: "{{ new_vm_ip.stdout }}"
        groups: new_vms
        ansible_user: vagrant
        ansible_port: 2222
	    ansible_private_key_file: >
	        .vagrant/machines/default/virtualbox/private_key
 
- name: Configure the newly added VM
  hosts: new_vms
  tasks:
    - name: Ping the new VM
      ansible.builtin.ping:

group_by

Used to create groups based on host variables during runtime.

---
- name: Group hosts by their OS distribution
  hosts: all
  tasks:
    - name: Collect OS facts
      ansible.builtin.setup:
 
    - name: Group hosts by distribution
      ansible.builtin.group_by:
        key: "os_{{ ansible_facts['distribution'] | lower }}"
 
- name: Perform tasks on Ubuntu hosts
  hosts: os_ubuntu
  tasks:
    - name: Install Nginx
      ansible.builtin.apt:
        name: nginx
        state: present
        update_cache: yes
      when: ansible_facts['distribution'] == 'Ubuntu'
 
- name: Perform tasks on CentOS hosts
  hosts: os_centos
  tasks:
    - name: Install Nginx
      ansible.builtin.yum:
        name: nginx
        state: present

👉 08. Ansible on Host Machine