Assistance With ternary and use of Jinja substitution for variable

352 views
Skip to first unread message

Clint Denham

unread,
Mar 26, 2021, 1:26:57 AM3/26/21
to ansible...@googlegroups.com
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

Dick Visser

unread,
Mar 26, 2021, 1:53:51 AM3/26/21
to ansible...@googlegroups.com
You were almost there!


On Fri, 26 Mar 2021 at 06:26, Clint Denham <clint...@gmail.com> wrote:
>
> 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')}] }}"

Inside ternary you are already in jinja mode, so  drop the curly braces, and concatenate with a tilde:

ports_list: "{{ ports_list|default([]) + [{'port-name': inventory_hostname + (place_index > 1) | ternary('1.' ~ place_index,'_mgmt')}] }}"

But, the index starts at zero. This should give you want you want:

ports_list: "{{ ports_list|default([]) + [{'port-name': inventory_hostname + (place_index > 0) | ternary('1.' ~ place_index,'_mgmt')}] }}"

--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Clint Denham

unread,
Mar 26, 2021, 3:10:18 AM3/26/21
to ansible...@googlegroups.com
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!

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAL8fbwPZTqgGmdNB8PEko9vfGEXV%3D98tKUppsxPzarNJ0YSmUA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages