Change variable in Ansible template based on group

2019-09-14 13:41发布

问题:

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

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

回答1:

What you should do is:
Provide default in template

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

Than override in group_vars

# group_vars/es-masters.yml
role_name_node_master: false