如何使用ansible在vars文件中包含vars文件? [英] How to include vars file in a vars file with ansible?

查看:33
本文介绍了如何使用ansible在vars文件中包含vars文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将 Ansible 中的 vars 文件动态包含到另一个 vars 文件中?

Is it possible to include a vars file in Ansible into another vars file dynamically?

即我有 vars 文件:

I.e. I have vars file:

---
definitions:
- { product: web_v2, suite: mysuite, include: default_step.yml }
- { product: prod2, suite: mysuite2, include: default_step.yml }

我希望将 default_step.yml 的内容添加到字典中.请注意,这是在 vars 文件中,因此有关如何从任务中包含 vars 文件的文档似乎并不适用.

I want the contents of default_step.yml to be added to the dictionary. Note this is in a vars file so the documentation on how to include a vars file from a task doesn't seem to apply.

我能想到的就是用 jinja 模板化这个文件并使用它的 {% include %} 函数,然后在主要任务中使用 include_vars 但那看起来很复杂...

All I can think of is to template this file with jinja and use its {% include %} function, and then afterwards use include_vars from the main task but that seems complicated...

推荐答案

不幸的是,vars 文件没有包含语句.

Unfortunately, vars files do not have include statements.

您可以将所有变量放入 definitions 字典中,也可以将这些变量作为另一个字典添加到同一文件中.

You can either put all the vars into the definitions dictionary, or add the variables as another dictionary in the same file.

如果您不想将它们放在同一个文件中,您可以通过在播放开始时添加 vars 文件将它们包含在剧本级别:

If you don't want to have them in the same file, you can include them at the playbook level by adding the vars file at the start of the play:

---
- hosts: myhosts

  vars_files:
    - default_step.yml

或在任务中:

---
- hosts: myhosts

  tasks:
    - name: include default step variables
      include_vars: default_step.yml

这篇关于如何使用ansible在vars文件中包含vars文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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