Change variable in Ansible template based on group - templates

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?

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

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

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

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

Related

How to show the first true element after if condition in for loop in django template?

I have a for loop in Django template. After that, I check for coincidences. But in some cases, there are might be 3 coincidences. I need to show only the first coincidence. Now, my code returns the name for 3 times, because, there are 3 coincidences
{% for ip in ips %}
{% if d.name == ip.name %}
<strong>{{ d.name}} </strong>
{% endif %}
{% endfor %}
SOLUTION
It is impossible to break forloop in django template, so I decided to change in views.py through queryset distinction of similar names
ips = Point.objects.defer('point').order_by('name').distinct('name')
I don't recommend doing this in Django Template , but in views itself. But if you can't then you can use {{ forloop|break }}.
Something like this :
{% for ip in ips %}
{% if d.name == ip.name %}
{{ forloop|break }}
<strong>{{ d.name}} </strong>
{% endif %}
{% endfor %}
Check the small snippet example here...

Django template nested for loop with if statement not working

I need to have a nested loop in my Django template, where the outer loop goes through a list of objects, and the inner loop goes through a list of those object id's, and I want to only do something for the id's on the inner list, it never executes however. I think it has something to do with the condition for the if statement, because if I replace it with a true statement it works but it doesn't work as it is now
(I have checked to see that the id's overlap)
{% for outer in outer_obj_list %}
{% for inner_id in inner_id_list %}
{% if outer.id == inner_id %}
// do something
console.log({{inner_id}});
console.log({{outer.id}});
{% endif %}
{% endfor %}
{% endfor %}
Syntax seems correct. I would just verbosely output everything.
Perhaps it should be something like this:
{% for main_obj in main_obj_list %}
main_obj: {{ main_obj }}
{% for obj_id in obj_id_list %}
obj_id: {{ obj_id}}
main_obj: {{ main_obj.id}}
{% if main_obj.id == obj_id %}
// do something
match: {{main_obj.id}} == {{obj_id}} ;
{% endif %}
{% endfor %}
{% endfor %}
Brother I am also face this problem so my clever mind get some clever solution about this problem we can do that with JavaScript easily so we need to run it in JavaScript and then.
{% for outer in outer_obj_list %}
{% for inner_id in inner_id_list %}
if(outer.id == inner_id.id ){
console.log({{inner_id.id}});
console.log({{outer.id}});
//And also if we reserve place in DOM then we can
//change the inner Html of them easily like.
//demo = document.getElementById("demo");
//demo.innerHTML = inner_Id.id or outer.id
}
{% endfor %}
{% endfor %}

Django execute block tag inside 'if' statement in template only if 'if' statement is true

I want to execute a block tag only if an if statement is true. Here is what I mean. This is my template:
{% extends "homePageBase.html" %}
{% if not blogPage %}
{% block isBlogFalse %} notBlogPage {% endblock %}
{% else %}
{% block isBlogTrue %} blogPage {% endblock %}
{% endif %}
But both the block tags get executed. Is there any way for the block tags to only get executed if the if / else statements are true? Any way around this issue or any way of accomplishing this task?
One answer to the problem can be found at https://stackoverflow.com/a/18638131/573392. To modify it for your situation, it would look like:
{% extends blogPage|yesno:"blogPage.html,notBlogPage.html" %}
This solution will allow you to load a template that is dependent on the blogPage variable, effectively achieving the outcome of the if statement.

How do I do display content in Django based on URL and all recurisive urls

So I have this in a Django template.
{% if request.get_full_path = "/blog/" %}
do something
{% endif %}
Which does exactly what I want when I go to http://somesite.com/blog/
What I want to do is also include all subdirectories.
So something like
{% if request.get_full_path = "/blog/*" %}
do something
{% endif %}
Unfortunately as far as I can tell Django doesn't do wildcards in templates. So how do I do this?
Try with this:
{% if request.get_full_path|slice:'0:6' == '/blog/' %}

PyCharm: auto-insert Django template closing-tags ( {% endif %} {% endfor %} , etc. ) option?

I've yet to find a setting that will enable auto-insertion of these closing tags, similar to how closing HTML tags are handled.
Is this even possible?
Just hit tab after if
{% if<tab>
and you get
{% if <cursor> %}
{% endif %}
This works for many tags: for, block, filter, spaceless, with, but inexplicably not blocktrans, autoescape, and comment changes to <comment></comment>