1

Hello Developer Community!

I'm currently working on developing some Ansible playbooks to manage Citrix NetScaler configuration and would like to get some help about the following. I have the following data structure defined in a YAML file:

prefix_header:                         "foo"
prefix_trailer:                        "bar"

nsapp_cs_vserver:
  - name:                              "testwebvserver-4_SSL_443"
    policybindings:
      - policyname:                    "TO_testwebservice-3"
        priority:                      "100"
      - policyname:                    "To-be-deleted"
        priority:                      "110"

I'm trying to find an easy way to dynamically convert the content of "policybindings" list variable to the following format: (I would like to append header and trailer prefixes to the actual value of "policyname")

    policybindings:
      - policyname:                    "foo_TO_testwebservice-3_bar"
        priority:                      "100"
      - policyname:                    "foo_To-be-deleted_bar"
        priority:                      "110"

I would like to use the policy names with header and trailer prefixes to invoke netscaler_cs_vserver Ansible module to configure Content Switching.

- name: "Bind CS policy(ies) to CS vServer(s) on ACTIVE node"
  netscaler_cs_vserver:
    name: "{{ prefix_header }}{{ item.name }}{{ prefix_trailer}}"
    policybindings: "{{ item.policybindings }}"
  register: bind_nsapp_cs_policy_result
  loop: "{{ nsapp_cs_vserver }}"

Could anybody please advise what is the correct and effective way to achieve this?

Many thanks in advance!

Belabacsi
  • 121
  • 2
  • 10

1 Answers1

1

It's possible to loop include_tasks to handle nested lists. For example the file

shell> cat convert-list.yml
- set_fact:
    policybindings: []
- set_fact:
    policybindings: "{{ policybindings +
                        [item|combine({'policyname':
                              item.policyname|
                              regex_replace( myregex, myreplace)})] }}"
  loop: "{{ outer_item.policybindings }}"
  vars:
    myregex: '^(.*)$'
    myreplace: "{{ prefix_header ~ '_\\1_' ~ prefix_trailer }}"

- set_fact:
    nsapp_cs_vserver2: "{{ nsapp_cs_vserver2|default([]) +
                           [outer_item|combine({'policybindings': policybindings})] }}"

included in the "outer loop" task

    - include_tasks: convert-list.yml
      loop: "{{ nsapp_cs_vserver }}"
      loop_control:
        loop_var: outer_item
    - debug:
        var: nsapp_cs_vserver2

give

    "nsapp_cs_vserver2": [
        {
            "name": "testwebvserver-4_SSL_443", 
            "policybindings": [
                {
                    "policyname": "foo_TO_testwebservice-3_bar", 
                    "priority": "100"
                }, 
                {
                    "policyname": "foo_To-be-deleted_bar", 
                    "priority": "110"
                }
            ]
        }
    ]
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Dear Vladimir! Thank You very much, it works like a charm :-) The only thing what I had to add to convert-list.yml file is `- set_fact: policybindings: []` as the first task to clean up the content of the fact before handling the next item in the loop. – Belabacsi Mar 22 '20 at 19:09
  • Right. To handle the next items from the outer loop. I've fixed it. – Vladimir Botka Mar 22 '20 at 20:23