Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an Ansible inventory include another?

We have a set of servers shared between different instances of an application and would like the list of them to be a separate file, with other -- instance-specific inventories -- including it.

(I know, this can be done with dynamic inventories, but those are code and we'd like our server-listings to remain data, so to speak.)

With INI-inventories this is impossible, but with YAML-ones it is tantalizingly close. For example, this answer shows, how this can be done by adding a handler for !include to Python's YAML-parser. One could then write:

all:
  group1:
    host1:
    host2:
  sharedservers: !include shared-servers.yaml

How can one add this functionality to one's own Ansible repository -- preferably, without implementing a whole new inventory-plugin (although inhering from Ansible's existing one would be Ok)?

like image 676
Mikhail T. Avatar asked Oct 31 '19 22:10

Mikhail T.


People also ask

Can we have multiple inventory files in Ansible?

If the location given to -i in Ansible is a directory (or as so configured in ansible. cfg ), Ansible can use multiple inventory sources at the same time. When doing so, it is possible to mix both dynamic and statically managed inventory sources in the same ansible run.

How do you use multiple inventory in Ansible?

Create a folder, add as many inventory files inside this folder and instruct Ansible to use this folder as the inventory (with -i folder_name or in your ansible. cfg file). All inventory files inside the folder will be merged into one (including scripts like ec2.py).

Is it possible to specify multiple inventory files at once?

You can also use multiple inventory files at the same time as described in Using multiple inventory sources, and/or pull inventory from dynamic or cloud sources or different formats (YAML, ini, and so on), as described in Working with dynamic inventory.

What Ansible inventory contains?

The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. The file can be in one of many formats depending on your Ansible environment and plugins. Common formats include INI and YAML.


1 Answers

To start with, your example inventory in your question does not respect the schema for yaml ansible inventory and will be declined parsing.

Now to answer your question, you can simply use several inventories at once. Here is a simple example:

I created 3 yaml inventory files:

  • inventories/hosts.yml
    ---
    group1:
      hosts:
        host1:
        host2:
    
  • inventories/otherhosts.yml
    ---
    group2:
      hosts:
        hostA:
        hostB:
    
  • and finally inventories/shared.yml
    ---
    sharedservers:
      hosts:
        host3:
        host4:
    

From there, it is fairly easy to address all needed hosts. The example below use ansible-inventory for a better output, but the -i option and target selection is the same whith ansible and ansible-playbook

  • Address all hosts in all inventory files inside inventory directory:
    $ ansible-inventory -i inventories/ all --graph
    @all:
      |--@group1:
      |  |--host1
      |  |--host2
      |--@group2:
      |  |--hostA
      |  |--hostB
      |--@sharedservers:
      |  |--host3
      |  |--host4
      |--@ungrouped:
    
    This is equivalent to calling each yaml files in a seperate -i option in this case
    ansible-inventory -i inventories/hosts.yml \
      -i inventories/otherhosts.yml -i inventories/shared.yml \
      all --graph
    
  • Address only specific inventories
    $ ansible-inventory -i inventories/hosts.yml \
      -i inventories/shared.yml all --graph
    @all:
      |--@group1:
      |  |--host1
      |  |--host2
      |--@sharedservers:
      |  |--host3
      |  |--host4
      |--@ungrouped:
    
    $ ansible-inventory -i inventories/otherhosts.yml \
      -i inventories/shared.yml all --graph
    @all:
      |--@group2:
      |  |--hostA
      |  |--hostB
      |--@sharedservers:
      |  |--host3
      |  |--host4
      |--@ungrouped:
    
like image 100
Zeitounator Avatar answered Nov 14 '22 03:11

Zeitounator