HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Creating ansible host_vars and group_vars files dynamically

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
dynamicallycreatinggroup_varsfileshost_varsandansible

Problem

Is it possible to populate host_vars and group_vars with results from a database like we do in dynamic inventories?

Actually I'm doing it inside my playbook either with lookups, either by using shell module and registering queries result.

If anyone has a more elegant way of populating variables like making an executable host_var file printing JSON to stdout or so ..

Solution

The most elegant way would be to use dynamic inventory, rather than static inventory and dynamic group variables.

However, if for some reason you don't want to do that, one other option is to use custom facts; these are scripts that live in /etc/ansible/facts.d on the client machines. Like dynamic inventory, they be executable scripts (and therefore, query redis or whatever else), but in this case they define variables just for that particular host. This isn't quite what you were looking for, since you'd have to query the database on each individual server instead of once for the entire group, but it can have a similar effect.

Context

StackExchange DevOps Q#3123, answer score: 2

Revisions (0)

No revisions yet.