Tasks are run against target servers. Some Ansible documentation refers to the target servers as "hosts".
After a clean install of Ansible, the "inventory" directive in ansible.cfg is commented out, like this.
#inventory = /path/to/hosts
In this scenario, the default hosts file is /etc/ansible/hosts and the default hosts file is completely commented out. If you were to issue command ansible all -m ping, the following would be displayed. Likewise, if you were to uncomment the "inventory" directive in ansible.cfg without defining your inventory, the following would be displayed.
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
Typically, target servers are defined in the default hosts file or your own inventory file. Sometimes, the "inventory" directive in ansible.cfg is uncommented and updated to point to the directory where the default hosts file or your own inventory file will be located.
Additionally, Ansible uses inventory plugins to parse inventory. For example, ansible.cfg may have the following.
[inventory]
enable_plugins = ansible.builtin.host_list, ansible.builtin.yaml, ansible.builtin.ini
The ansible command with the --list-hosts flag will display the managed nodes in your inventory. Or, the ansible-inventory --list or ansible-inventory --graph commands could be used as well.
After a clean install of Ansible, the default hosts file and inventory in the ansible.cfg file are commented out. With the default hosts file and inventory in the ansible.cfg file commented out, the ansible all --list-hosts command will return the following.
~]$ ansible all --list-hosts
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
hosts (0):
To resolve this, you will need to define your inventory. Refer to Ansible - Defining your inventory. After you have defined your inventory, ansible all --list-hosts command should return your entire inventory. Or, you could use "unmanaged" or a custom inventory group. Refer to Ansible - Inventory groups (all | ungrouped | other).
Be aware that if you do not use the -i command line option, the default hosts file will be used.
ansible all --list-hosts
The -i command line option followed by an inventory file can be used to return the hosts from the specified inventory file.
ansible all -i /path/to/inventory/file --list-hosts
Something like this should be returned.
hosts (11):
all:
hosts:
server1.example.com:
server2.example.com:
children:
linux:
server3.example.com:
server4.example.com:
windows:
server5.example.com:
server6.example.com:
Ansible uses different plugins to parse the default hosts file or your own inventory file, such as the host_list, yaml, or ini plugin. The -vvv flag can be used to determine the plugin that was used.
ansible all --list-hosts -vvv
In this example, the yaml plugin was used to parse the /etc/ansible/hosts file.
Parsed /etc/ansible/hosts inventory source with yaml plugin
Did you find this article helpful?
If so, consider buying me a coffee over at