5

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 ..

chicks
  • 1,911
  • 1
  • 13
  • 29
storm
  • 1,759
  • 3
  • 16
  • 34

1 Answers1

2

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.

Xiong Chiamiov
  • 2,841
  • 1
  • 10
  • 30