Bootstrap FreeKB - Ansible - advanced_host_list inventory plugin
Ansible - advanced_host_list 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. 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. This assumes you have yaml as one of the enabled_plugins in ansible.cfg. Let's say you have advanced_host_list as one of the enabled_plugins in ansible.cfg.

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

 

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

AVOID TROUBLE

You must include the trailing comma after the managed node hostname

 

ansible all -i "server[1:10].example.com," --list-hosts -vvv

 

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

Unable to parse address from hostname, leaving unchanged: Detected range in host but was asked to ignore ranges

 

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

Parsed server[1:10].example.com, inventory source with advanced_host_list plugin

 

Something like this should be returned.

  hosts (10):
    server1.example.com
    server2.example.com
    server3.example.com
    server4.example.com
    server5.example.com
    server6.example.com
    server7.example.com
    server8.example.com
    server9.example.com
    server10.example.com

 

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

ansible-inventory -i "server[1:10].example.com," --list

 

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

ansible-inventory -i "server[1:10].example.com," --list

 

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

ansible-inventory -i "server[1:10].example.com," --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 52b9ad in the box below so that we can be sure you are a human.