FreeKB - Ansible ansible-inventory --list command
Ansible - ansible-inventory --list command

The ansible-inventory command with the --list option can be used to display your inventory of managed hosts. Or, the ansible --list-hosts 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-inventory --list command will return the following.

Also, the ansible-inventory command uses the YAML plugin to parse your default hosts file or your own inventory file. If your default hosts file or your own inventory file is using some other format, such as the INI format, the following will be returned.

[WARNING]:  * Failed to parse /etc/ansible/hosts with yaml plugin: YAML inventory has invalid structure, it should be a dictionary, got: <class
'ansible.parsing.yaml.objects.AnsibleUnicode'>
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: Could not match supplied host pattern, ignoring: all
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    },
    "ungrouped": {}
}

 

The above error is commonly resolved be defining your inventory using the YAML file format, like this.

all:
  hosts:
    server1.example.com:
    server2.example.com:
  children:
    linux:
      hosts:
        server3.example.com:
        server4.example.com:
    windows:
      hosts:
        server5.example.com:
        server6.example.com:

 

Be aware that if you do not use the -i command line option, the default hosts file will be used.

ansible-inventory --list

 

The -i command line option followed by an inventory file can be used to return the hosts from the specified inventory file.

ansible-inventory -i /path/to/inventory/file --list

 

Something like this should be returned.

{
    "_meta": {
        "hostvars": {
            "server1.example.com": {},
            "server2.example.com": {},
            "server3.example.com": {},
            "server4.example.com": {},
            "server5.example.com": {},
            "server6.example.com": {}
        }
    },
    "all": {
        "children": [
            "linux",
            "ungrouped",
            "windows"
        ]
    },
    "linux": {
        "hosts": [
            "server3.example.com",
            "server4.example.com"
        ]
    },
    "ungrouped": {},
    "windows": {
        "hosts": [
            "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-inventory --list -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 5a033 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   |