Search code examples
ansibleinventory

User-customizable Ansible inventory


Let's say our product is made of two components - consumer and aggregator. Each installed node has n consumers and 1 aggregator.

I want to allow customers to make their own custom configuration of up to X clusters, varying the n of consumers per each cluster.

My inventory concept:

( implied
  [cluster1_aggregator]
  [cluster2_aggregator]
  and so on for other groups 
)

[clusterX_aggregator]
machine1.example.com

[clusterX_consumer]
machine2.example.com
machine3.example.com

[clusterX_all:children]
clusterX_consumer
clusterX_aggregator

[site:children]
cluster1_all
cluster2_all
...
clusterX_all

I thought I'd prepare all the ":children" groups in the installation package and then allow the customer to fill in only the machines he has available, so for example only 2 clusters (so there would be cluster3_all:children present in their hosts inventory, but not cluster3_aggregator group).

I use roles almost exclusively in my playbooks. Is there a way I can automatically run this playbook only with non-empty inventory groups? I saw the possibly relevant answers in Ansible - Define Inventory at run time, but 1) those expect user interaction, which I would very much like to avoid, and 2) I need to have the aggregators and consumers grouped per node, can't have one huge group of consumers and take first X as that question has.

All clusterX would be looped for 1 to and then there would be a role with hosts:site that would do some configuration on top of all deployed clusters.

Expected outcome:

  • Cluster1 has some hosts filled in [cluster1_aggregator] and [cluster1_consumer], so it's deployed.
  • Cluster2 has some hosts filled in [cluster2_aggregator] and [cluster2_consumer], so it's deployed.
  • Cluster3 doesn't have any hosts filled in [cluster3_aggregator] or [cluster3_consumer], so it's not deployed. The same apllies for remaining clusters 4 to X.
  • As cluster2 was the last cluster to be deployed, the final site-wide configuration is done for all machines from cluster1 and cluster2.

Is the following possible in such an environment?

  • to detect the highest filled out inventory section
  • loop roles over the number-indexed host groups

Solution

  • I think you have to take a look at inventory scripts, as it sounds like they can do what you're looking for.

    You can write such script in the language of your choice and it can return only the valid clusters by your definition.