Ansible - 包含来自远程主机的 vars 文件 [英] Ansible - include vars file from remote host

查看:16
本文介绍了Ansible - 包含来自远程主机的 vars 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想包含远程主机上文件中的变量,而不是运行 Ansible 的控制机器.

I would like to include variables from a file on the remote host, rather than the control machine Ansible is running on.

例如我有一个文件/var/database_credentials.yml(在我的网络服务器上)

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?

include_vars 模块只从控制机器获取文件.我可以使用 fetch 模块,但这似乎是不必要的步骤.

The include_vars module only takes files from the control machine. I could use the fetch module but that seems like an unnecessary step.

推荐答案

将其与 /etc/ansible/facts.d 集成应该不难.

It should not be hard to integrate that with /etc/ansible/facts.d.

您可以在该目录中存储 JSON 文件、INI 文件或可执行脚本,并且在执行安装模块后,内容/输出将作为服务器事实可用.

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.

我不确定它是否需要 YAML.您可能很幸运,只需将符号链接添加到您的文件 /var/database_credentials.yml 即可.(文档中没有提到 YAML,但如果不支持 YAML 会很奇怪,因为 Ansible 中的几乎所有内容都基于 YAML)如果没有,您可以使用您喜欢的语言创建一个脚本,该脚本读取该文件并输出一个 JSON对象.

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.

请参阅本地事实 (Facts.d)在文档中.

这篇关于Ansible - 包含来自远程主机的 vars 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆