在 Ansible 主机文件中将主机定义为变量 [英] Defining host as variable in Ansible hosts file

查看:29
本文介绍了在 Ansible 主机文件中将主机定义为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的hosts 文件

I have a rather simple hosts file

[clients]
qas0062
[dbs_server]
qas0063

对于项目的用户,我们不希望他们修改 hosts 文件,而是我们有一个单独的 user.config.yml 文件,其中包含各种用户-可配置的参数.在那里我们有条目,例如

For the users of the project we don't want them to modify hosts file but rather we have a separate user.config.yml file that contain various user-configurable parameters. There we have entry such as

dbs_server: qas0065

所以问题是:是否可以在 hosts 文件中使用一个变量来使用 user.config.yml 中定义的值?格式是什么?

So the question is: is it possible to use a variable in the hosts file that would use a value defined in user.config.yml? And what would be the format?

推荐答案

很确定您无法模板化清单中的实际主机密钥条目,但您可以模板化其 ansible_host 连接的值var 来达到大致相同的效果,例如:

Pretty sure you can't templatize the actual host key entry in the inventory, but you can templatize the value of its ansible_host connection var to achieve roughly the same effect, eg:

[clients]
clienthost ansible_host="{{ clienthost_var }}"
[dbs_server]
dbsserver ansible_host="{{ dbsserver_var }}"

然后在播放开始执行之前从外部变量设置这些变量的值(例如,使用 vars_files 指令或 -e).

then set the value of those vars from external vars before the play starts executing (eg, with the vars_files directive or -e).

这篇关于在 Ansible 主机文件中将主机定义为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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