Bootstrap FreeKB - Ansible - yaml inventory plugin
Ansible - yaml inventory plugin

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. For example, ansible.cfg may have the following. Let's say you have yaml as one of the enabled_plugins in ansible.cfg.

[inventory]
enable_plugins = host_list, yaml, ini

 

This also assumes you have defined your inventory in a yaml file, such as hosts.yml. Let's say hosts.yml contains the following managed hosts (e.g. the target systems).

linux:
  hosts:
    server1.example.com:
    server2.example.com:
windows:
  hosts:
    server3.example.com:
    server4.example.com:

 

The ansible command with the --list-hosts and -vvv flags can be used to determine if the yaml plugin is being used.

ansible all --list-hosts -vvv

 

If you get something like this, this suggests you did not define yaml in ansible.cfg properly.

[WARNING]: Unable to parse /etc/ansible/hosts.yml as an inventory source

 

If the following is returned, the host_list plugin is being used.

Parsed /etc/ansible/hosts.yml inventory source with yaml plugin

 

Something like this should be returned.

Parsed /etc/ansible/hosts.yml inventory source with yaml plugin
  hosts (4):
    server1.example.com
    server2.example.com
    server3.example.com
    server4.example.com

 

Additionally, you may use the -i or --inventory option to point to a specific YAML file that contains your target servers.

ansible all --inventory /tmp/hosts.yml --list-hosts -vvv

 

Or, the ansible-inventory --list command can be used.

ansible-inventory --list

 

Or, the ansible-inventory --graph command can be used.

ansible-inventory --graph

 

 




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 860337 in the box below so that we can be sure you are a human.