Inventory
The inventory tells Ansible which hosts to manage. It can be a simple text file, a YAML file, or a script that dynamically queries your cloud provider.
Static Inventory (INI Format)
Section titled “Static Inventory (INI Format)”The simplest form — a file listing hosts and groups:
[webservers]web1.example.comweb2.example.com192.168.1.50
[dbservers]db1.example.comdb2.example.com
[loadbalancers]lb1.example.comRun against this inventory:
ansible -i inventory.ini webservers -m pingansible-playbook -i inventory.ini site.ymlStatic Inventory (YAML Format)
Section titled “Static Inventory (YAML Format)”The same inventory in YAML:
all: children: webservers: hosts: web1.example.com: web2.example.com: 192.168.1.50: dbservers: hosts: db1.example.com: db2.example.com: loadbalancers: hosts: lb1.example.com:YAML format is more verbose but better for complex inventories with nested variables.
Groups
Section titled “Groups”Every host belongs to at least two groups:
all— Every host in the inventory.ungrouped— Hosts not in any other group.
Group of Groups (Children)
Section titled “Group of Groups (Children)”[webservers]web1.example.comweb2.example.com
[dbservers]db1.example.com
[production:children]webserversdbserversNow production contains all hosts from webservers and dbservers.
YAML equivalent:
all: children: production: children: webservers: hosts: web1.example.com: web2.example.com: dbservers: hosts: db1.example.com:Host and Group Variables
Section titled “Host and Group Variables”Inline Variables (INI)
Section titled “Inline Variables (INI)”[webservers]web1.example.com ansible_port=2222 http_port=8080web2.example.com http_port=80Inline Variables (YAML)
Section titled “Inline Variables (YAML)”webservers: hosts: web1.example.com: ansible_port: 2222 http_port: 8080 web2.example.com: http_port: 80Variable Files (Recommended)
Section titled “Variable Files (Recommended)”For cleaner organization, use host_vars/ and group_vars/ directories:
inventory/ hosts.yml group_vars/ all.yml # variables for all hosts webservers.yml # variables for webservers group dbservers.yml # variables for dbservers group host_vars/ web1.example.com.yml # variables for this specific hosthttp_port: 80app_env: productiondeploy_user: deployansible_port: 2222http_port: 8080Connection Variables
Section titled “Connection Variables”Special variables that control how Ansible connects:
| Variable | Default | Purpose |
|---|---|---|
ansible_host | inventory hostname | IP or hostname to connect to |
ansible_port | 22 | SSH port |
ansible_user | current user | SSH username |
ansible_ssh_private_key_file | — | Path to SSH key |
ansible_become | false | Enable privilege escalation (sudo) |
ansible_become_method | sudo | How to escalate (sudo, su, doas) |
ansible_python_interpreter | /usr/bin/python3 | Python path on the remote host |
[webservers]web1.example.com ansible_user=deploy ansible_become=truePatterns (Targeting Hosts)
Section titled “Patterns (Targeting Hosts)”Patterns let you target specific hosts or groups:
ansible webservers -m ping # all hosts in webserversansible dbservers -m ping # all hosts in dbserversansible all -m ping # every hostansible web1.example.com -m ping # single hostansible 'webservers:dbservers' -m ping # union (both groups)ansible 'webservers:&production' -m ping # intersectionansible 'webservers:!web1.example.com' -m ping # exclude a hostansible '*.example.com' -m ping # wildcardThe same patterns work in playbooks:
- hosts: webservers:&production tasks: - name: Deploy app # ...Dynamic Inventory
Section titled “Dynamic Inventory”Instead of a static file, a script or plugin queries your infrastructure and returns host lists in JSON.
AWS Example (aws_ec2 Plugin)
Section titled “AWS Example (aws_ec2 Plugin)”plugin: amazon.aws.aws_ec2regions: - us-east-1filters: tag:Environment: productionkeyed_groups: - key: tags.Role prefix: role - key: placement.availability_zone prefix: azcompose: ansible_host: public_ip_addressansible-inventory -i inventory/aws_ec2.yml --graph # see discovered hostsansible -i inventory/aws_ec2.yml role_webserver -m pingOther Dynamic Inventory Plugins
Section titled “Other Dynamic Inventory Plugins”azure.azcollection.azure_rm— Azure VMsgoogle.cloud.gcp_compute— GCP instanceskubernetes.core.k8s— Kubernetes podscommunity.docker.docker_containers— Docker containers
Default Inventory
Section titled “Default Inventory”Set a default inventory in ansible.cfg so you don’t need -i every time:
# ansible.cfg[defaults]inventory = ./inventory/hosts.ymlKey Takeaways
Section titled “Key Takeaways”- Inventory defines what Ansible manages — hosts, groups, and their variables.
- Use
group_vars/andhost_vars/directories for clean variable management. - Patterns let you target flexible subsets of hosts (
webservers:&production,all:!staging). - Use dynamic inventory plugins for cloud environments — hosts are discovered automatically.
- Set a default inventory in
ansible.cfgto avoid passing-ion every command.