Bootstrap FreeKB - Ansible - ansible-inventory --graph command
Ansible - ansible-inventory --graph command

Updated:   |  Ansible articles

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. The ansible-doc command can be used to list the inventory plugins that can be used with the version of Ansible you are using.

~]$ ansible-doc --type inventory --list
ansible.builtin.advanced_host_list Parses a 'host list' with ranges                                                                                                                                                                 
ansible.builtin.auto               Loads and executes an inventory plugin specified in a YAML config                                                                                                                                
ansible.builtin.constructed        Uses Jinja2 to construct vars and groups based on existing inventory                                                                                                                             
ansible.builtin.generator          Uses Jinja2 to construct hosts and groups from patterns                                                                                                                                          
ansible.builtin.host_list          Parses a 'host list' string                                                                                                                                                                      
ansible.builtin.ini                Uses an Ansible INI file as inventory source                                                                                                                                                     
ansible.builtin.script             Executes an inventory script that returns JSON                                                                                                                                                   
ansible.builtin.toml               Uses a specific TOML file as an inventory source                                                                                                                                                 
ansible.builtin.yaml               Uses a specific YAML file as an inventory source

 

For example, ansible.cfg may have the following.

[inventory]
enable_plugins = ansible.builtin.host_list, ansible.builtin.yaml, ansible.builtin.ini

 

The ansible-inventory command with the --graph option can be used to display a graph of your inventory of managed hosts. Or, the ansible --list-hosts or ansible-inventory --list commands could be used as well.

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
@all:
  |--@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 --graph

 

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 --graph

 

Something like this should be returned.

@all:
  |--@linux:
  |  |--server3.example.com
  |  |--server4.example.com
  |--@ungrouped:
  |--@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-inventory --graph -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 Buy Me A Coffee



Comments


Add a Comment


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