On 2/27/20 7:24 PM, Vladimir Botka wrote:
> 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.

Amazing! Thanks for the insight.

Regards
        Racke

> 
> HTH,
> 
>       -vlado
> 


-- 
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

-- 
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/9fc1e64e-8754-a710-e6a3-df686fbcdbe4%40linuxia.de.

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to