在任务中访问ansible.cfg变量 [英] Access ansible.cfg variable in task
问题描述
如何在任务中引用在ansible.cfg
中定义的remote_tmp
(或任何其他)值?例如,在my_task/defaults/main.yml
:
How can I refer remote_tmp
(or any other) value defined in ansible.cfg
in my tasks? For example, in the my_task/defaults/main.yml
:
file_ver: "1.5"
deb_file: "{{ defaults.remote_tmp }}/deb_file_{{ file_ver }}.deb"
产生错误:
fatal: [x.x.x.x]: FAILED! => {"failed": true,
"msg": "the field 'args' has an invalid value,
which appears to include a variable that is undefined.
The error was: {{ defaults.remote_tmp }}/deb_file_{{ file_ver }}.deb:
'defaults' is undefined\... }
推荐答案
您不能开箱即用.
您需要动作插件或vars插件来读取不同的配置参数.
如果您采用动作插件方式,则必须调用新创建的动作才能定义remote_tmp
.
如果选择vars插件方式,则在清单初始化期间将remote_tmp
与其他主机vars一起定义.
You can't do this out of the box.
You either need action plugin or vars plugin to read different configuration parameters.
If you go action plugin way, you'll have to call your newly created action to get remote_tmp
defined.
If you choose vars plugin way, remote_tmp
is defined with other host vars during inventory initialization.
示例./vars_plugins/tmp_dir.py
:
from ansible import constants as C
class VarsModule(object):
def __init__(self, inventory):
pass
def run(self, host, vault_password=None):
return dict(remote_tmp = C.DEFAULT_REMOTE_TMP)
请注意,vars_plugins
文件夹应位于您的hosts
文件附近,或者应在ansible.cfg中明确定义它.
Note that vars_plugins
folder should be near your hosts
file or you should explicitly define it in your ansible.cfg.
您现在可以使用以下方法进行测试:
You can now test it with:
$ ansible localhost -i hosts -m debug -a "var=remote_tmp"
localhost | SUCCESS => {
"remote_tmp": "$HOME/.ansible/tmp"
}
这篇关于在任务中访问ansible.cfg变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!