Ansible group_vars [英] Ansible group_vars

查看:832
本文介绍了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/中,我将拥有:

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 }}

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

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

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