On Thu, 27 Feb 2020 07:13:09 -0800 (PST)
jean-christophe manciot <[email protected]> wrote:

>   - key_1: '$.*?/|\^(){}+@[]&_-'
>     key_2:
>       - '$.*?/|\^(){}+@[]&_-'
>       - "{\_}"
>       - value23
>     key_3: value31
>     key_4:
>       - value41
>       - value42
>     key_5: value51
>   - key_1_: value12
>     _key_2:
>       - value24
>       - value25
>     key__3: '$.*?/|\^(){}+@[]&_-'
>     key_5___: value52
> 
> - to replace all '_' from the keys into '-', except for the first character 
> which may be a '_';
> 
> - all values must remain untouched
> - the name and number of the keys are variable
> - the depth of "recursiveness" is variable and unlimited
> 
> For instance here, the translation would result as:
>   - key-1: '$.*?/|\^(){}+@[]&_-'
>     key-2:
>       - '$.*?/|\^(){}+@[]&_-'
>       - "{\_}"
>       - value23
>     key-3: value31
>     key-4:
>       - value41
>       - value42
>     key-5: value51
>   - key-1-: value12
>     _key-2:
>       - value24
>       - value25
>     key--3: '$.*?/|\^(){}+@[]&_-'
>     key-5---: value52

Given the list is stored in variable "my_list" the task below does the job

    - set_fact:
        my_list2: "{{ my_list2|default([]) +
                      [dict(my_keys_fixed2|zip(my_values))] }}"
      vars:
        my_keys: "{{ item.keys()|list }}"
        my_values: "{{ item.values()|list }}"
        my_keys_fixed1: "{{ my_keys|
                            map('regex_replace', '_', '-')|
                            list }}"
        my_keys_fixed2: "{{ my_keys_fixed1|
                            map('regex_replace', '^-(.*)$', '_\\1')|
                            list }}"
      loop: "{{ my_list }}"

This task will replace '-' in the first character of the keys if found.

HTH,

        -vlado

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200227192434.0f3db5c5%40gmail.com.

Attachment: pgplr9l546khg.pgp
Description: OpenPGP digital signature

Reply via email to