Dynamic inventory allows Ansible to programmatically retrieve host and group data at runtime by querying external sources, such as CMDBs or cloud provider APIs. This eliminates the need for manual updates, ensuring that your inventory stays current.
Converting a Static Inventory to a Dynamic Inventory Script
Assume you have a static inventory as shown above. To convert it into a dynamic inventory, you can create a Python script—say,inventory.py—that outputs the inventory data in JSON format. You can then run your playbook with either the static file or the dynamic script:
inventory.py script might look like:
--list argument, it produces the complete inventory in JSON format:
--host argument is passed, the script should return the specific host’s variables in JSON format. Although this example is basic, it effectively demonstrates the concept of a dynamic inventory in Ansible. In production environments, you might enhance the script to fetch live inventory data from a CMDB or cloud service API.
In certification exams, you are not typically required to write such inventory scripts. However, you should understand how these scripts function and supply inventory data to Ansible.
Using Dynamic Inventory with Cloud Providers
Dynamic inventory is especially useful when managing cloud instances. For instance, to manage AWS instances, you can use the EC2 inventory script. After downloading theec2.py script from the Ansible GitHub repository, run your playbook with the following command:
Other Inventory Formats and Plugins
In addition to static (INI) and dynamic (Python script) inventories, Ansible also supports YAML inventory files. For example, the same inventory data can be expressed in YAML as follows:- INI plugin: Reads inventory data from INI files.
- Script plugin: Executes a script and interprets its JSON output as inventory.
- YAML plugin: Interprets YAML-formatted files.
/etc/ansible/ansible.cfg, under the [inventory] section:
Scripts vs. Plugins
While the dynamic inventory script is a tried-and-true method, Ansible now recommends using inventory plugins. Although inventory scripts are simple and can be implemented in any language, inventory plugins offer better integration with the Ansible ecosystem and additional functionality. Consider the following example of an advanced dynamic inventory script written in Python:Testing Your Inventory
Debugging your inventory configuration is simple with theansible-inventory command. This utility displays how Ansible interprets your inventory data. For example, when testing the EC2 inventory script, you might run:
Using the
ansible-inventory command is an excellent way to troubleshoot issues with inventory variables and to ensure that your dynamic inventory is working as expected.That concludes our exploration of dynamic inventory in Ansible. Experiment with both static and dynamic inventories to find the best approach for your environment, and leverage Ansible’s extensive inventory plugins for a streamlined and scalable automation experience.