在任务中访问ansible.cfg变量

编程入门 行业动态 更新时间:2024-10-24 20:22:42
本文介绍了在任务中访问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变量

本文发布于:2023-11-12 01:37:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   ansible   cfg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!