Assistance With ternary and use of Jinja substitution for variable

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

You were almost there!

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!