In Jinja2 template file, I am trying to assign a value to variable
"pod_num". But I am unable to get this
If (servers_grp1) is in mainservers_1
then
pod_num = 2
else
pod_num = 4
I tried a couple of things but did not work out.
{% if hosts is in groups[mainservers_1]
{% set pod_num = 2 %}
{% else %}
{% set pod_num = 4 %}
This work great, but the variable is only available in the template, not outside of it.
You'll see it work by this code
{% if hosts is in groups[mainservers_1]
{% set pod_num = 2 %}
{% else %}
{% set pod_num = 4 %}
{{ pod_num }}
If you need the variable in the scope of Ansible global you should check out group_vars.
Create a file group_vars/servers_grp1.yml and group_vars/servers_grp1.yml with the appropriate and variables with values.