principleMinor
Compare Strings to get Unique values in Jinja2
Viewed 0 times
uniquejinja2getvaluescomparestrings
Problem
I have a variable with list of node IP's and its respective Availability Zone.
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
or
Is this possible to perform such operations of comparing strings and get its respective value in Jinja template? Anything would help. Thanks in advance.
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.
Solution
Yes, this can be done. The following should do the trick:
To test it with https://cryptic-cliffs-32040.herokuapp.com/, you can use the following JSON:
You might give the documentation a read if you are going to be doing much jinja templating. I have found it indispensible.
"{%- 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.
Code Snippets
"{%- 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%}"{
"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"
}
}Context
StackExchange DevOps Q#2906, answer score: 2
Revisions (0)
No revisions yet.