I would like to include variables from a file on the remote host, rather than the control machine Ansible is running on.
For example I have a file /var/database_credentials.yml (on my webserver)
What's the best way to add variables from that file to hostvars so that I can use them in a template?
The include_vars module only takes files from the control machine. I could use the fetch module but that seems like an unnecessary step.
It should not be hard to integrate that with /etc/ansible/facts.d
.
You can store JSON files, INI files or executable scripts in that directory and the content/output will be available as server facts after the setup module was executed.
I'm not sure it will take YAML. You might be lucky and it'll work to simply add a symlink to your file /var/database_credentials.yml
. (YAML is not mentioned in the docs but it would be strange if YAML is not supported since pretty much everything in Ansible is based on YAML) If not, you can create a script in the language you prefer which reads that file and outputs a JSON object.
See Local Facts (Facts.d) in the docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With