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

2
  • 1
    What is the use case? Why not just create dynamic inventory script? Jan 19, 2018 at 13:01
  • We have env variables for each server stored in Redis .. actually I can make a dynamic inventory script and get variables directly in the inventory but the fact that all our servers are physical machines I was thinking that it would be better to make a static inventory and shift the variables (retrieved dynamically) to host_vars and group_vars as recommanded in the doc ..
    – storm
    Jan 19, 2018 at 13:13

1 Answer 1

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.

1
  • Well seems that it's not possible to make a dynamic group_vars files with ansible, I don't want to use facts for this , I will put variables into inventory script first then make a custom script to create group_vars files
    – storm
    Feb 7, 2018 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.