14

I've got an Ansible inventory file a bit like this:

[es-masters]
host1.my-network.com

[es-slaves]
host2.my-network.com
host3.my-network.com

[es:children]
es-masters
es-slaves

I also have a Jinja2 template file that needs a certain value set to "true" if a host belongs to the "es-masters" group.

I'm sure that there's a simple way of doing it but after some Googling and reading the documentation, I've drawn a blank.

I'm looking for something simple and programmatic like this to go in the Jinja2 template:

{% if hostvars[host][group] == "es-masters" %}
node_master=true
{% else %}
node_master=false
{% endif %}

Any ideas?

3
  • Are you passing hostvars to the render function or do you expect jinja2 to be able to access hostvars? Aug 28, 2014 at 16:07
  • That was just an example, really - what I'm really looking for is a programmatic way to "see" the groups that a particular host is a member of and find a way to change a variable in a template based on that. Aug 29, 2014 at 8:47
  • Docs: In addition to storing variables directly in the INI file, host and group variables can be stored in individual files relative to the inventory file. These variable files are in YAML format. See YAML Syntax if you are new to YAML. The YAML library is included with the AppEngine SDK
    – voscausa
    Aug 29, 2014 at 14:48

4 Answers 4

22

You do it the other way around. You check if the identifier (hostname or IP or whatever is in your inventory) is in the defined group. Not if the group is in the hostvars.

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

But, what you better should do is this:

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
1
10

If your inventory does not identify hosts with ansible_fqdn, ansible_hostname, etc., you can also use group_names to check if the current host has "es-masters" as one of its groups.

{% if 'es-masters' in group_names %}
node_master=true
{% else %}
node_master=false
{% endif %}

See https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables

5

To avoid error with non existing group you should check first if the group exists:

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

maybe like this? change the var which named node_master, rather than use a txt

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.