FreeKB - Ansible --list-hosts command line option
Ansible - --list-hosts command line option

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.

Refer to Ansible - Getting Started with Inventory if you are unfamiliar with inventory.

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter b9719 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |