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)?
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.
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).
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.
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.
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:
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
$ 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
$ 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With