Bootstrap FreeKB - Ansible - Inventory in ansible.cfg
Ansible - Inventory in ansible.cfg

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.

[inventory]
enable_plugins = host_list, yaml, ini

 

In this scenario, the hosts_lists inventory plugin allows you to pass one or more target servers on the command line using the -i or --inventory option.

AVOID TROUBLE

When the -i or --inventory option only contains one target system, you MUST INCLUDE A TRAILING COMMA after the hostname or IP address of the target system.

 

In this example, the example.yml playbook would only be run against server1.example.com.

ansible-playbook example.yml --inventory server1.example.com,

 

Here is how to do the same using the ansible ad hoc command.

ansible --inventory server1.example.com, --module-name ping

 

And here is how to run a playbook or ad hoc command against two (or more) managed hosts. When you have two (or more) hosts, you do not need to end with a trailing comma.

ansible-playbook example.yml --inventory server1.example.com,server2.example.com

 

The yaml inventory_plugin allows you to define target servers in a YAML default hosts file or your own inventory file. For example, let's say you have an default hosts file or your own inventory file named inventory.yml that contains target systems, perhaps something like this.

all:
  hosts:
    server1.example.com:
    server2.example.com:
    server3.example.com:
    server4.example.com:

 

Here is how you can run the example.yml playbook using the target systems specified in inventory.yml.

ansible-playbook example.yml --inventory /path/to/inventory.yml

 




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