根据有孩子的组更改 Ansible 模板中的变量? [英] Change variable in Ansible template based on group with children?

查看:19
本文介绍了根据有孩子的组更改 Ansible 模板中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 根据组更改 Ansible 模板中的变量.但是,如何在有孩子的小组中扩展答案?

I found Change variable in Ansible template based on group. However how do I extend the answer in groups with children?

如果我将上面链接中的组变量文件扩展为

If I extend the group vars file in the link above to

[host0]
host0.my-network.com
[host1]
host1.my-network.com

[es-masters:children]
host0
host1

如何扩展上述帖子的答案(如下所示)以使其适用于上面的组 vars 文件?简单地将 FQDN 放在 jinja2 语句中是行不通的.

How can I extend the above post's answer (shown below) to make it work with the group vars file above? Simply putting the FQDN in the jinja2 statement does not work.

{% if ansible_fqdn in groups['es-masters'] %}
node_master=true
{% else %}
node_master=false
{% endif %}

推荐答案

你应该做的是:
在模板中提供默认值

What you should do is:
Provide default in template

# role_name/templates/template.j2
node_master={{ role_name_node_master | default(true) }}

比在 group_vars 中覆盖

Than override in group_vars

# group_vars/es-masters.yml
role_name_node_master: false

这篇关于根据有孩子的组更改 Ansible 模板中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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