14. Dynamic Inventory

14.1. Dynamic inventory scripts

  • Ansible supports other inventory sources via an external inventory system

  • many inventory scripts are available in the github repo. Here are some examples:

    • zabbix
    • vmware
    • spacewalk
    • openstack
    • nagios
    • gce
    • ec2
    • azure
    • cobbler
  • you can write your own inventory script in any language

    • return JSON in the right format
    • fill up the the proper arguments
  • reload inventory during a playbook run

- name: reload inventory, useful with dynamic inventories when play makes changes to the existing hosts
  cloud_guest:            # this is fake module
    name: newhost
    state: present

- name: Refresh inventory to ensure new instaces exist in inventory
  meta: refresh_inventory

Hint

Checkout the tuning tipps in the Ansible documentation.

14.1.1. How to use dynamic inventory

  • Ansible reads the configured inventory path
  • if the file is executable, Ansible will execute it
  • as example download a dynmic inventory script e.g ec2.py
  • change the permissions -> chmod +x

Hint

It’s possible to set a folder as inventroy. And there you can mix static & dynamic inventories.

  • a mixed inventory example
inventory/
├── [-rwxr-xr-x]  ec2.py
├── [-rw-r--r--]  static
└── [-rwxr-xr-x]  zabbix.py