如何在vars文件中包含vars文件? [英] How to include vars file in a vars file with ansible?
问题描述
是否可以将Ansible中的vars文件动态地包含到另一个vars文件中?
Is it possible to include a vars file in Ansible into another vars file dynamically?
即我有vars文件:
---
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
这篇关于如何在vars文件中包含vars文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!