ansible - Append string to each list items stored in a variable

1.4k Views Asked by At

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!

1

There are 1 best solutions below

2
On BEST ANSWER

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"
                }
            ]
        }
    ]