Inventory File

Ansible uses an inventory file to provide a list of hosts to Ansible. These hosts can be seperated into groups, or individual in the inventory file. The default inventory file is located at /etc/ansible/hosts but you can supply your own and point to it using the -i <inventory_file> option when running an Ansible playbook or command. The inventory file is usually written in INI format.


In the inventory file hosts by themselves are the simplest to define. A simple inventory file can look like this:


When using groups in your inventory file you can classify your hosts and decide what groups of hosts you want to control from Ansible. You can also include a single host in multiple groups. Such as the being a webserver and a database at the same time.

Groups are defined like this:



There is one catch though. When using groups we cannot place single hosts not included with any groups below. So the following will apply:

# my single hosts

# Put Groups below


Non-standard SSH Ports

If you are running non-standard SSH ports on your hosts you can also specify the port on the hostname seperated by a colon. For example:

Host Patterns

In an inventory file we can also define multiple hosts with one entry. You can specify numeric ranges, as well as alphabetic ranges.

Numeric range example:


Alphabetic range example:


Host Entry Components

<alias> <special variables> <variables>
host alias:
can be a hostname or just an alias, if using an alias, you will need to specify the special variable ansible_host=
special variables:
there are many of these, they include the Behavioral Inventory Parameters
these are variables you want to define specifically to a host that would be used in your playbooks

Using Hosts not in Inventory

When looking to use hosts without an inventory file, we can specify the ansible-playbook or ansible ad-hoc command as

ansible-playbook -i hostname, playbook.yml

If you noticed we added the , after the hostname. This specifies to Ansible that we want to use a comma separated list of hosts not related to the hosts file.

Behavioral Inventory Parameters

These are also known commonly as Behavioral Inventory Parameters and can all be found here: