# Assistance With ternary and use of Jinja substitution for variable

**URL:** <https://forum.ansible.com/t/assistance-with-ternary-and-use-of-jinja-substitution-for-variable/34861>\
**Category:** Ansible Project\
**Created:** [March 26, 2021, 5:26am UTC](https://forum.ansible.com/t/assistance-with-ternary-and-use-of-jinja-substitution-for-variable/34861 "2021-03-26T05:26:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clint\_Denham](https://avatars.discourse-cdn.com/v4/letter/c/919ad9/32.png) [@Clint\_Denham](https://forum.ansible.com/u/Clint_Denham)\
**Post date:** [March 26, 2021, 5:26am UTC](https://forum.ansible.com/t/assistance-with-ternary-and-use-of-jinja-substitution-for-variable/34861/1 "2021-03-26T05:26:26Z")

</div>

The main point of this I am having difficulty in using ternary in conjunction with a variable and would like to see if this is even possible? The ternary doesn’t seem to like the use of variable substitution inside its statements - at least how I’m using them.

This is the list I’m trying to create

ports\_list: “{{ ports\_list|default() + [{‘port-name’: inventory\_hostname + (place\_index \> 1) | ternary(‘1.{{place\_index}}’,‘\_mgmt’)}] }}”

Ultimately in plain English, what I am trying to do is reference the existing port\_order list and…

- For the first port, have it named {{inventory\_hostname}}\_mgmt
- For all remain ports, have the ports named {{inventory+hostname}}\_1.{{place-index}} onto the end of it so I would end up with {{inventory\_hostname}}\_1.1, etc…

The task is…

- name: Populate Port Lists With management and other ports  
set\_fact:  
ports\_list: “{{ ports\_list|default() + [{‘port-name’: inventory\_hostname + (place\_index \> 1) | ternary(‘1.{{place\_index}}’,‘\_mgmt’)}] }}”  
loop: “{{ port\_order }}”  
loop\_control:  
index\_var: place\_index

This works fine for the first entry of the listand I end up with…

device-name\_mgmt

For the remaining ports however, I end up with the literal string…

device-name\_{{place\_index}} - what I am expecting is device-name\_1.1

Clearly my knowledge of Jinja is lacking but I can’t sort this out

I am working with OpenStack, and this is the list of dictionaries for the network ports that need to be attached to a server instance

i.e.  
“port\_order”: [  
{  
“int\_type”: “normal”,  
“subnet”: “vlan3555.v6”,  
“type”: “mgmt”  
},  
{  
“int\_type”: “normal”,  
“subnet”: “vlan704.v4”,  
“type”: “provider”  
},  
{  
“int\_type”: “normal”,  
“subnet”: “vlan3444.v6”,  
“type”: “tenant”  
}

This list of dictionaries is built as the main reference for all of the Ansible tasks so its fairly important this structure doesn’t change, but if there’s no way around it I’ll update it

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [March 26, 2021, 5:53am UTC](https://forum.ansible.com/t/assistance-with-ternary-and-use-of-jinja-substitution-for-variable/34861/2 "2021-03-26T05:53:29Z")

</div>

You were almost there!

---

<div class="post-metadata">

**Author:** ![Clint\_Denham](https://avatars.discourse-cdn.com/v4/letter/c/919ad9/32.png) [@Clint\_Denham](https://forum.ansible.com/u/Clint_Denham)\
**Post date:** [March 26, 2021, 7:09am UTC](https://forum.ansible.com/t/assistance-with-ternary-and-use-of-jinja-substitution-for-variable/34861/3 "2021-03-26T07:09:51Z")

</div>

Oh wow! Thanks a ton! That works perfect. I hadn’t seen/heard of the tilde usage yet. Lots to learn but many thanks (again) for the fix!
