在任务中访问 ansible.cfg 变量 [英] Access ansible.cfg variable in task

查看:34
本文介绍了在任务中访问 ansible.cfg 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的任务中引用 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屋!

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