Ansible group_vars [英] Ansible group_vars

查看:33
本文介绍了Ansible group_vars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动将 sensu 检查部署到主机扮演的每个角色.

I'm trying to automate the deployment of sensu checks to each role that a host plays.

我目前有一个像

group_vars/
  nginx
  all

在每个 group_vars 文件中,我定义了以下内容:

In each group_vars file, I have defined the following:

sensu_checks:
  - check_name
  - check_other_name

例如,在 group_vars/all 中:

So for example, in group_vars/all I'd have:

sensu_checks:
  - check_raid
  - check_load
  - check_disk

在 group_vars/nginx 中我有:

In group_vars/nginx I'd have:

sensu_checks:
 - check_pid
 - check_http

我想知道是否有可能获得特定主机应安装的所有检查,例如:

What I would like to know if it's possible would be to get all the checks that a specific host should install, for example with:

- name: Print host sensu checks
  command: echo {{item}}
  with_flattened:
   - {{ sensu_checks }}

但这不起作用,因为它只给我定义主机名的最后一个组的 group_vars.有没有办法获得一个扁平列表,其中包含主机附加到的所有组的检查?

This doesn't work though, as it only gives me the group_vars of the last group the host name is defined in. Is there a way to get a flattened list the checks of all the groups the host is attached to?

在前面的例子中,我希望

In the previous example, I'd expect

 [ check_load, check_disk, check_raid, check_http, check_pid ] 

但我得到了

[ check_http, check_pid ]

对于 nginx 主机(它是 'all' 和 'nginx' 组的一部分)

for an nginx host (which is part of both the 'all' and and 'nginx' groups)

推荐答案

with_flattened 在这种情况下没有达到您的预期 - 您是 变量范围.

with_flattened doesn't do what you expect in this case - you're a victim of variable scoping.

nginx 组是最具体的,因此 ansible 正在使用该变量定义 - 这解释了为什么您只获得 nginx<中定义的 sensu_check/代码>.

The nginx group is the most specific, so ansible is using that variable definition - which explains why you're only getting the sensu_check defined in nginx.

您可以在两个位置之一重命名 var(我推荐 nginx var,因为这是最具体的一个),然后使用 with_flattened 组合两个列表:

You could rename the var in one of the two places (I recommend the nginx var, since that's the most specific one), and then use with_flattened to combine the two lists:

with_flattened:- {{ sensu_checks }}- {{ sensu_nginx_checks }}

这篇关于Ansible group_vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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