Inventory Plugins
Inventory plugins allow users to point at data sources to compile the inventory of hosts that Ansible uses to target tasks, either via the -i /path/to/file
and/or -i 'host1, host2'
command line parameters or from other configuration sources.
Enabling Inventory Plugins
Most inventory plugins shipped with Ansible are disabled by default and need to be whitelisted in youransible.cfg file in order to function. This is how the default whitelist looks in theconfig file that ships with Ansible:
- [inventory]
- enable_plugins = host_list, script, yaml, ini, auto
This list also establishes the order in which each plugin tries to parse an inventory source. Any plugins left out of the list will not be considered, so you can ‘optimize’ your inventory loading by minimizing it to what you actually use. For example:
- [inventory]
- enable_plugins = advanced_host_list, constructed, yaml
Using Inventory Plugins
The only requirement for using an inventory plugin after it is enabled is to provide an inventory source to parse.Ansible will try to use the list of enabled inventory plugins, in order, against each inventory source provided.Once an inventory plugin succeeds at parsing a source, any remaining inventory plugins will be skipped for that source.
To start using an inventory plugin with a YAML configuration source, create a file with the accepted filename schema for the plugin in question, then add plugin: plugin_name
. Each plugin documents any naming restrictions. For example, the aws_ec2 inventory plugin:
- # demo.aws_ec2.yml
- plugin: aws_ec2
Or for the openstack plugin:
- # clouds.yml
- plugin: openstack
The auto
inventory plugin is enabled by default and works by using the plugin
field to indicate the plugin that should attempt to parse it. You can configure the whitelist/precedence of inventory plugins used to parse source using the ansible.cfg [‘inventory’] enable_plugins
list. After enabling the plugin and providing any required options you can view the populated inventory with ansible-inventory -i demo.aws_ec2.yml —graph
:
- @all: |[email protected]ec2: | |—ec2-12-345-678-901.compute-1.amazonaws.com | |—ec2-98-765-432-10.compute-1.amazonaws.com |<a href=”https://docs.ansible.com/cdn-cgi/l/email-protection“ class=”_cf_email“ data-cfemail=”5b76761b2e353c29342e2b3e3f”>[email protected]:
You can set the default inventory path (via inventory
in the ansible.cfg [defaults] section or the ANSIBLE_HOSTS
environment variable) to your inventory source(s). Now running ansible-inventory —graph
should yield the same output as when you passed your YAML configuration source(s) directly. You can add custom inventory plugins to your plugin path to use in the same way.
Your inventory source might be a directory of inventory configuration files. The constructed inventory plugin only operates on those hosts already in inventory, so you may want the constructed inventory configuration parsed at a particular point (such as last). Ansible parses the directory recursively, alphabetically. You cannot configure the parsing approach, so name your files to make it work predictably. Inventory plugins that extend constructed features directly can work around that restriction by adding constructed options in addition to the inventory plugin options. Otherwise, you can use -i
with multiple sources to impose a specific order, e.g. -i demo.aws_ec2.yml -i clouds.yml -i constructed.yml
.
You can create dynamic groups using host variables with the constructed keyed_groups
option. The option groups
can also be used to create groups and compose
creates and modifies host variables. Here is an aws_ec2 example utilizing constructed features:
- # demo.aws_ec2.yml
- plugin: aws_ec2
- regions:
- - us-east-1
- - us-east-2
- keyed_groups:
- # add hosts to tag_Name_value groups for each aws_ec2 host's tags.Name variable
- - key: tags.Name
- prefix: tag_Name_
- separator: ""
- groups:
- # add hosts to the group development if any of the dictionary's keys or values is the word 'devel'
- development: "'devel' in (tags|list)"
- compose:
- # set the ansible_host variable to connect with the private IP address without changing the hostname
- ansible_host: private_ip_address
Now the output of ansible-inventory -i demo.aws_ec2.yml —graph
:
- @all: |[email protected]ec2: | |—ec2-12-345-678-901.compute-1.amazonaws.com | |—ec2-98-765-432-10.compute-1.amazonaws.com | |—… |[email protected]: | |—ec2-12-345-678-901.compute-1.amazonaws.com | |—ec2-98-765-432-10.compute-1.amazonaws.com |[email protected]NameECSInstance: | |—ec2-98-765-432-10.compute-1.amazonaws.com |<a href=”https://docs.ansible.com/cdn-cgi/l/email-protection“ class=”__cf_email“ data-cfemail=”2f02026f5b4e48”>[email protected]_Name_Test_Server: | |—ec2-12-345-678-901.compute-1.amazonaws.com |<a href=”https://docs.ansible.com/cdn-cgi/l/email-protection“ class=”__cf_email“ data-cfemail=”c7eaea87b2a9a0b5a8b2b7a2a3”>[email protected]
If a host does not have the variables in the configuration above (i.e. tags.Name
, tags
, private_ip_address
), the host will not be added to groups other than those that the inventory plugin creates and the ansible_host
host variable will not be modified.
Plugin List
You can use ansible-doc -t inventory -l
to see the list of available plugins.Use ansible-doc -t inventory <plugin name>
to see plugin-specific documentation and examples.
- advanced_host_list – Parses a ‘host list’ with ranges
- auto – Loads and executes an inventory plugin specified in a YAML config
- aws_ec2 – ec2 inventory source
- aws_rds – rds instance source
- azure_rm – Azure Resource Manager inventory plugin
- constructed – Uses Jinja2 to construct vars and groups based on existing inventory.
- foreman – foreman inventory source
- gcp_compute – Google Cloud Compute Engine inventory source
- generator – Uses Jinja2 to construct hosts and groups from patterns
- host_list – Parses a ‘host list’ string
- ini – Uses an Ansible INI file as inventory source.
- k8s – Kubernetes (K8s) inventory source
- nmap – Uses nmap to find hosts to target
- openshift – OpenShift inventory source
- openstack – OpenStack inventory source
- scaleway – Scaleway inventory source
- script – Executes an inventory script that returns JSON
- tower – Ansible dynamic inventory plugin for Ansible Tower.
- virtualbox – virtualbox inventory source
- vmware_vm_inventory – VMware Guest inventory source
- vultr – Vultr inventory source
- yaml – Uses a specific YAML file as an inventory source.
See also
- About Playbooks
- An introduction to playbooks
- Callback Plugins
- Ansible callback plugins
- Connection Plugins
- Ansible connection plugins
- Filters
- Jinja2 filter plugins
- Tests
- Jinja2 test plugins
- Lookups
- Jinja2 lookup plugins
- Vars Plugins
- Ansible vars plugins
- User Mailing List
- Have a question? Stop by the google group!
- irc.freenode.net
ansible IRC chat channel