1

I'm working on setting up some automation for an F5 BigIP load balancer. When creating the virtual server, I have a variable containing the various profiles I want to include and also a few that should always be included.

I want to use this variable:

 domains:
   - foo.example.com
   - bar.example.com
   - baz.example.com

In the following module under profiles. Note, I can't do a loop because that would replace the value each time. I want all list items from domains to be expanded in the single execution of this task. I've tried using a Jinja for loop but it just hangs when I try to execute.

 - name: Configure virtual server
   f5networks.f5_modules.bigip_virtual_server:
     state: present
     partition: Common
     name: insite-ssl
     destination: 10.10.10.10
     port: 443
     pool: example-pool
     snat: Automap
     description: Testing VIP
     profiles: |
       {% for domain in domains %}
       - name: {{ domain }}
         context: client-side
       {% endfor %}
       - http-SSL-XForwarded
       - name: example.com_wildcard
         context: client-side
     provider: "{{ f5_conn }}"
   tags:
     - vip

What is the right way to solve this?

1 Answer 1

1

By using this construct, you are actually creating a string, not a list.
This can be tested doing:

- set_fact:
    what_am_I: |
      {% for domain in domains %}
      - name: {{ domain }}
        context: client-side
      {% endfor %}
      - http-SSL-XForwarded
      - name: example.com_wildcard
        context: client-side

- debug:
    var: what_am_I is string

Which gives:

TASK [set_fact] ******************************************************************
ok: [localhost]

TASK [debug] *********************************************************************
ok: [localhost] => 
  what_am_I is string: true

You could use json_query in order to create a list of dictionaries out of your domains list, something like:

- name: Configure virtual server
  f5networks.f5_modules.bigip_virtual_server:
    state: present
    partition: Common
    name: insite-ssl
    destination: 10.10.10.10
    port: 443
    pool: example-pool
    snat: Automap
    description: Testing VIP
    profiles: |
      {{ domains | json_query("[].{name: @, context: 'client-side'}") + extra_domains }}
    provider: "{{ f5_conn }}"
  tags:
    - vip
  vars:
    extra_domains:
      - http-SSL-XForwarded
      - name: example.com_wildcard
        context: client-side
Sign up to request clarification or add additional context in comments.

4 Comments

After installing jmespath this worked perfectly - thank you for the explanation why my attempt wasn't working and for a workable solution!
Follow-up question on this. The requirements have changed on my and now my domains list is all top-level-domain domains, eg example.com. The requirement is to be able to support adding both the top-level-domain AND a www.top-level-domain to the profiles.
Given your solution, how would I add an additional json_query and modify the value for name to append 'www' to it? I tried adding an additional json_query within the curly braces but that gave an error. + domains | community.general.json_query("[].{name: 'www.' + @, context: 'client-side'}" but that
I’d suggest you to open a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.