Answers: <p>The most elegant way would be to use dynamic inventory, rather than static inventory and dynamic group variables.</p>
  
  <p>However, if for some reason you don't want to do that, one other option is to use <a href="https://medium.com/@jezhalford/ansible-custom-facts-1e1d1bf65db8" rel="nofollow noreferrer">custom facts</a>; these are scripts that live in <code>/etc/ansible/facts.d</code> 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 <em>quite</em> 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.</p>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-07T10:51:27.580Z
Answers: 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
Answers: storm
Answers: CC BY-SA 3.0
Answers: 2018-01-26T23:10:38.100Z
Answers: 2018-01-26T23:10:38.100Z
Answers: Xiong Chiamiov
Body: <p>Is it possible to populate <code>host_vars</code> and <code>group_vars</code> with results from a database like we do in dynamic inventories?</p>
  
  <p>Actually I'm doing it inside my playbook either with lookups, either by using <code>shell</code> module and registering queries result.</p>
  
  <p>If anyone has a more elegant way of populating variables like making an executable <code>host_var</code> file printing JSON to stdout or so ..</p>
  
Comments: CC BY-SA 3.0
Comments: 2018-01-19T13:01:34.100Z
Comments: What is the use case? Why not just create dynamic inventory script?
Comments: Konstantin Suvorov
Comments: CC BY-SA 3.0
Comments: 2018-01-19T13:13:29.333Z
Comments: 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 ..
Comments: storm
Tags: <ansible><automation><databases>
Title: Creating ansible host_vars and group_vars files dynamically
UserDisplayName: storm
ThreadText: # Creating ansible host_vars and group_vars files dynamically
  
  (Asked by: username_0 on 2018-01-19)
  
  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 ..
  
  **Tags:** ansible, automation, databases
  
  > **Comments:**
  > **username_1**: What is the use case? Why not just create dynamic inventory script?  
  > **username_0**: 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 ..  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-01-26. Score: 2 | Accepted answer
  
  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](https://medium.com/@jezhalford/ansible-custom-facts-1e1d1bf65db8); 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.
  
  > **Comments:**
  > **username_0**: 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  
  
  ---
  
  