Using Multiple Inventories

Working with Multiple Inventory Files

  • Ansible supports working with multiple inventory files.
  • One way of using multiple inventory files is to enter multiple -i parameters with the ansible or ansible-playbook commands to specify the name of the files to be used.
  • ansible-inventory -i inventory -i listing101.py --list
    • Would produce an output list based on the static inventory in the inventory file, as well as the dynamic inventory that is generated by the listing101.py Python script.
  • You can also specify the name of a directory using the -i option.
    • Uses all files in the directory as inventory files.
    • When using an inventory directory, dynamic inventory files still must be executable for this approach to work.

Lab: Using Multiple Inventories

  • Open a shell as the ansible user and create a directory with the name inventories.
  • Copy the file listing101.py to the directory inventories.
  • Also copy the inventory file to the directory inventories.
  • To make sure both inventories have some unique contents, add the following lines to the file inventories/inventory:
webserver1
webserver2
  • Add the following lines to the Linux /etc/hosts file:
192.168.4.203    ansible3.example.com    ansible3
192.168.4.204    ansible4.example.com    ansible4
  • Use the command ansible-inventory -i inventories --list.