Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible Jinja2 string comparison

I am getting value of variable "env" in Jinja2 template file using a variable defined in group_vars like:

env: "{{ defined_variable.split('-')[0] }}"

env possible three values could be abc, def, xyz.

On the basis of this value I want to use server URL, whose possible values I have defined inside defaults/main.yml as:

server_abc: https://xxxx.xxx.com
server_def: https://xxxxx.xxx.com
server_xyz: https://xxxx.xxx.com

In Jinja2 template, I am trying to do:

{% if 'abc'  == "{{env}}" %}
serverURL: '{{ server_abc }}'
{% elif 'def'  == "{{env}}" %}
serverURL: '{{ server_def}}'
{% elif 'xyz' == "{{env}}" %}
 serverURL: '{{ server_xyz }}'
{% else %}
ServerURL: 'server Url not found'
{% endif %}

However it is always ending up defining ServerURL = "server URL not found" even if env comes with value of abc, def or xyz.

If I try to replace env in Jinja2 template (hardcoded) like below condition does satisfy to true:

     {% if 'abc'  == "abc" %}
     serverURL: '{{ server_abc }}' 

So that implies me syntax is true but the value of "{{env}}" at run time is not evaluated.

Any suggestion what can I do to solve this?

like image 831
Ruchir Bharadwaj Avatar asked Oct 17 '16 12:10

Ruchir Bharadwaj


People also ask

Does Ansible support Jinja2 tests?

Like all templating, tests always execute on the Ansible controller, not on the target of a task, as they test local data. In addition to those Jinja2 tests, Ansible supplies a few more and users can easily create their own. Test syntax¶. Test syntax varies from filter syntax (variable | filter).

How do I use multiple conditions in Ansible?

When you have multiple conditions that all need to be true (that is, a logical and ), you can specify them as a list: If a fact or variable is a string, and you need to run a mathematical comparison on it, use a filter to ensure that Ansible reads the value as an integer:

What tests and filters does Ansible use in conditionals?

Ansible uses Jinja2 tests and filters in conditionals. Ansible supports all the standard tests and filters, and adds some unique ones as well.

How to get the symmetric difference of 2 lists in Ansible?

To get the symmetric difference of 2 lists (items exclusive to each list): 1.6 新版功能. To compare a version number, such as checking if the ansible_distribution_version version is greater than or equal to ‘12.04’, you can use the version_compare filter. The version_compare filter can also be used to evaluate the ansible_distribution_version:


1 Answers

You don't need quotes and braces to refer to variables inside expressions. The correct syntax is:

{% if 'abc' == env %}
serverURL: '{{ server_abc }}'
{% elif 'def' == env %}
serverURL: '{{ server_def }}'
{% elif 'xyz' == env %}
serverURL: '{{ server_xyz }}'
{% else %}
ServerURL: 'server URL not found'
{% endif %}

Otherwise you compare two strings, for example abc and {{env}} and you always get a negative result.

like image 180
techraf Avatar answered Oct 20 '22 00:10

techraf