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

查看:37
本文介绍了在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天全站免登陆