Bootstrap FreeKB - Ansible - constructed inventory plugin
Ansible - constructed 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. Let's say you have yaml and constructed in enabled_plugins in ansible.cfg.

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

 

The constructed inventory plugin is often used to construct a group of target servers from two (or more) YAML inventory files. For example, let's say foo.yml contain the following.

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

 

And bar.yml contains the following.

all:
  hosts:
    server5.dev.example.com:
    server6.dev.example.com:
    server7.prod.example.com:
    server8.prod.example.com:

 

And you want to run your playbook against the "dev" or "prod" servers in both foo.yml and bar.yml. In this scenario, you could create a YAML file named constructed.yml that contains the following.

---
plugin: ansible.builtin.constructed
groups:
  dev: inventory_hostname is search ('dev')
  prod: inventory_hostname is search ('prod')

 

And your playbook would have -hosts: dev to run the playbook against the "dev" servers in foo.yml and bar.yml (or prod for prod).

---
- hosts: dev
  tasks:
  - file:
      path: /tmp/foo.txt
      state: touch
...

 

Last but not least, when running your playbook you would include both inventory files that contain the target servers (foo.yml and bar.yml) followed by the constructed.yml file.

ansible-playbook testing.yml -i foo.yml -i bar.yml -i constructed.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 93c80f in the box below so that we can be sure you are a human.