在 Ansible 中,如何将不同文件中的变量合并到一个数组中? [英] In Ansible, how to combine variables from separate files into one array?

查看:33
本文介绍了在 Ansible 中,如何将不同文件中的变量合并到一个数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Ansible 中,在一个角色中,我有这样的 vars 文件:

In Ansible, in a role, I have vars files like this:

vars/
    app1.yml
    app2.yml

每个文件都包含特定于应用程序/网站的变量,如下所示:

Each file contains vars specific to an app/website like this:

name: app1
git_repo: https://github.com/philgyford/app1.git
# ...

理想情况下,如果任务事先不知道哪些应用程序具有可变文件,我想最终得到一个名为 apps 的数组,如下所示:

Ideally, without the task knowing in advance which apps have variable files, I'd like to end up with an array called apps like this:

apps:
  - name: app1
    git_repo: https://github.com/philgyford/app1.git
    # ...
  - name: app2
    git_repo: https://github.com/philgyford/app2.git
    # ...

即,将文件中的变量合二为一.

ie, that combines the variables from the files into one.

我知道我可以像这样加载所有变量文件:

I know I can load all the variable files like this:

- name: Load var files
  with_fileglob:
    - ../vars/*.yml
  include_vars: '{{ item }}'

但由于每个文件都有相同的变量名,它会覆盖之前的每组变量.我看不到加载变量并将它们放入 apps 数组的方法.

But given each file has identical variable names, it will overwrite each previous set of variables. I can't see a way to load the variables and put them into an apps array.

如果这是使这样的事情成为可能的唯一方法,我愿意稍微重新安排一下.

I'm open to rearranging things slightly if it's the only way to make something like this possible.

推荐答案

你不能那样做.变量将始终覆盖具有相同名称的变量.您可以使用此确切设置做的唯一事情是编写您自己的 vars 插件 读取这些文件并将它们合并到一个数组中.

You can not do that. Variables will always override variables with the same name. The only thing you could do with this exact setup is to write your own vars plugin which reads those files and merges them into an array.

如果您愿意更改应用定义的结构,您可以使用哈希和 设置您的hash_behavior=merge.在每个 vars 文件中,您都会有如下定义:

If you are open to change the structure of your apps definition you can use a hash and set your hash_behavior=merge. In each vars file then you'd have a definition like:

apps:
  app1:
    git_repo: https://github.com/philgyford/app1.git

<小时>

apps:
  app2:
    git_repo: https://github.com/philgyford/app2.git

当 Ansible 加载这两个文件时,它会自动将它们合并到一起:

When Ansible loads both files it will merge it automatically together into:

apps:
  app1:
    git_repo: https://github.com/philgyford/app1.git
  app2:
    git_repo: https://github.com/philgyford/app2.git</pre>

但请注意,hash_behavior=merge 从根本上改变了 Ansible 在全局层面的默认行为.确保您的所有角色都没有此设置的问题.文档提到:

But be advised that hash_behavior=merge fundamentally changes the default behavior of Ansible on a global level. Make sure all your roles do not have issues with this setting. The documentation mentions:

我们通常建议不要使用此设置,除非您认为绝对需要它

We generally recommend not using this setting unless you think you have an absolute need for it

如果您仍然使用 Ansible 1,则可以改用我的旧插件之一:include_vars_merged.基本上,这将 hash_behavior=merge 的行为添加到单个任务中.

If you still use Ansible 1 you could use one of my old plugins instead: include_vars_merged. Basically this adds the behavior of hash_behavior=merge to only a single task.

我还没有考虑将它迁移到 Ansible 2,目前看来我不再需要它了.

这篇关于在 Ansible 中,如何将不同文件中的变量合并到一个数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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