<p>Yes, this can be done. The following should do the trick:</p>

<pre><code>"{%- for ip, az in seed.iteritems() %} 
  {%- if 'us-east-1a' in az %}
    {%- if firstloop is not defined %}
    {%- set firstloop = 1 %}
{{- ip }}
    {%- else %}
{{- " " + ip}}
    {%- endif %}    
  {%- endif %}
{%- endfor%}"
</code></pre>

<p>To test it with <a href="https://cryptic-cliffs-32040.herokuapp.com/" rel="nofollow noreferrer">https://cryptic-cliffs-32040.herokuapp.com/</a>, you can use the following JSON:</p>

<pre><code>{
    "seed": {
      "10.18.13.12": "us-east-1a",
      "10.18.37.93": "us-east-1b", 
      "10.18.68.147": "us-east-1a",
      "10.18.21.55": "us-east-1b"
    }
}
</code></pre>

<p>You might give the <a href="http://jinja.pocoo.org/docs/2.10/templates/" rel="nofollow noreferrer">documentation</a> a read if you are going to be doing much jinja templating. I have found it indispensible.</p>

CC BY-SA 3.0
2017-12-14T21:13:30.933Z
Thank you for your reply. I am new to Jinja and not able to progress on this challenging task. I tested it out on https://cryptic-cliffs-32040.herokuapp.com/ and getting few errors. It would be great if you can help me solve it
Nahush
CC BY-SA 3.0
2017-12-14T21:21:37.940Z
@Nahush - Nice tool! I've definitely done things like this before. I'll update my answer in a few min with a working example.
James Shewey
CC BY-SA 3.0
2017-12-14T22:05:04.243Z
@Nahush - That should do it. Aside from the typos, I had to account for the fact that variables set in a loop are scoped for the life of the loop, and are thus vars set/modified in the loop are unavailable outside the loop. There is a workaround for this using .append() but it is not available on the Jinja version used in your tool, but the above works.
James Shewey
CC BY-SA 3.0
2017-12-14T22:06:53.517Z
Also worth noting: if you needed all IPs for us-east, you would change `{%- if 'us-east-1a' in az %}` to `{%- if 'us-east' in az %}`
James Shewey
CC BY-SA 3.0
2017-12-14T22:38:25.777Z
that is really great work. I think I am getting IP's from same AZ rather than IP's from unique AZ. I wanted IP's to be one from us-east-1a and one from us-east-1b.
Nahush
CC BY-SA 3.0
2017-12-14T21:08:29.367Z
2017-12-14T22:00:07.407Z
2017-12-14T22:00:07.407Z
James Shewey
<p>I have a variable with list of node IP's and its respective Availability Zone.</p>

<pre><code>seed = {
  10.18.13.12 = us-east-1a
  10.18.37.93 = us-east-1b
  10.18.68.147 = us-east-1a
  10.18.21.55 = us-east-1b
}
</code></pre>

<p>I will be using this variable in Jinja template but want to filter out the variable based on unique Avalability Zones and get the IP. I want to compare the Avalability Zones to get unique values and have IP from each unique Avalability Zone. For instance I want the output in form of</p>

<pre><code>"10.18.13.12, 10.18.37.93" 
</code></pre>

<p>or</p>

<pre><code>"10.18.68.147, 10.18.21.55"
</code></pre>

<p>Is this possible to perform such operations of comparing strings and get its respective value in Jinja template? Anything would help. Thanks in advance.</p>

<ansible><jinja>
Compare Strings to get Unique values in Jinja2
Nahush
# Compare Strings to get Unique values in Jinja2

(Asked by: username_0 on 2017-12-14)

I have a variable with list of node IP's and its respective Availability Zone.
seed = {
  10.18.13.12 = us-east-1a
  10.18.37.93 = us-east-1b
  10.18.68.147 = us-east-1a
  10.18.21.55 = us-east-1b
}

I will be using this variable in Jinja template but want to filter out the variable based on unique Avalability Zones and get the IP. I want to compare the Avalability Zones to get unique values and have IP from each unique Avalability Zone. For instance I want the output in form of
"10.18.13.12, 10.18.37.93" 

or
"10.18.68.147, 10.18.21.55"

Is this possible to perform such operations of comparing strings and get its respective value in Jinja template? Anything would help. Thanks in advance.

**Tags:** ansible, jinja


---

## 1 answers

### Answer by username_1 on 2017-12-14. Score: 2

Yes, this can be done. The following should do the trick:
"{%- for ip, az in seed.iteritems() %} 
  {%- if 'us-east-1a' in az %}
    {%- if firstloop is not defined %}
    {%- set firstloop = 1 %}
{{- ip }}
    {%- else %}
{{- " " + ip}}
    {%- endif %}    
  {%- endif %}
{%- endfor%}"

To test it with https://cryptic-cliffs-32040.herokuapp.com/, you can use the following JSON:
{
    "seed": {
      "10.18.13.12": "us-east-1a",
      "10.18.37.93": "us-east-1b", 
      "10.18.68.147": "us-east-1a",
      "10.18.21.55": "us-east-1b"
    }
}

You might give the documentation a read if you are going to be doing much jinja templating. I have found it indispensible.

> **Comments:**
> **username_0**: Thank you for your reply. I am new to Jinja and not able to progress on this challenging task. I tested it out on https://cryptic-cliffs-32040.herokuapp.com/ and getting few errors. It would be great if you can help me solve it  
> **username_1**: @username_0 - Nice tool! I've definitely done things like this before. I'll update my answer in a few min with a working example.  
> **username_1**: @username_0 - That should do it. Aside from the typos, I had to account for the fact that variables set in a loop are scoped for the life of the loop, and are thus vars set/modified in the loop are unavailable outside the loop. There is a workaround for this using .append() but it is not available on the Jinja version used in your tool, but the above works.  
> **username_1**: Also worth noting: if you needed all IPs for us-east, you would change `{%- if 'us-east-1a' in az %}` to `{%- if 'us-east' in az %}`  
> **username_0**: that is really great work. I think I am getting IP's from same AZ rather than IP's from unique AZ. I wanted IP's to be one from us-east-1a and one from us-east-1b.  

---

