How to replace all occurrences of '_' by '-' in all keys of a YAML complex object

100 views
Skip to first unread message

jean-christophe manciot

unread,
Feb 27, 2020, 10:13:09 AM2/27/20
to Ansible Project

For instance, let's consider the following YAML data:

  - 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


The goal is:

- 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

Any suggestion?

Karl Auer

unread,
Feb 27, 2020, 11:12:42 AM2/27/20
to ansible-project
Are you seeking to change only LHS literals in declarations, or do you need to change such literals in arbitrary other locations in your YAML?

Basically, any tool for making the change you want needs to be able to clearly identify where the change is needed and where not - for example, NOT in the RHS (values) of the declarations in your examples.

If you have a relatively small number of keys that need changing, you could simply process them all by name using something like sed:

   change key_1 to key-1
   change key_2 to key-2
   change _key_3 to _key-3

and so on. You might be able to do something more heroic with sed, but in my experince simple sed is best.

If you have too many target keys to do this, then you will need a programmatic solution rather than a declarative solution (I think).

There may well be a tool out there that understands YAML and can perform transforms on it - it would be worth spending a bit of time looking for one before you work on your own solution. There are definitely tools for working with JSON (jq for example). You may be able to convert YAML to JSON, do the transforms with a JSON-capable tool, then convert back to YAML. A quick flirt with Google suggests that there are a great many converters, some online and some not.

If you do decide to do it yourself, it is probably untimately simplest to use Python or some other language with good JSON-handling libraries.

Regards, K.

--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/000d7d79-ddbe-45c9-89c4-2eef73979b2f%40googlegroups.com.


--

Karl Auer { manager, systems support }
P: 1300 759 975
E: ka...@2pisoftware.com
2pisoftware.com

GPG/PGP : 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E
Previous:
958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816

Vladimir Botka

unread,
Feb 27, 2020, 1:24:55 PM2/27/20
to jean-christophe manciot, ansible...@googlegroups.com
On Thu, 27 Feb 2020 07:13:09 -0800 (PST)
jean-christophe manciot <actionm...@gmail.com> 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

Stefan Hornburg (Racke)

unread,
Feb 28, 2020, 3:21:57 AM2/28/20
to ansible...@googlegroups.com
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.

signature.asc

jean-christophe manciot

unread,
Mar 2, 2020, 9:50:57 AM3/2/20
to Ansible Project
There is an issue: the values of key-1, key-2 and key--3 are modified. It seems that the translation does not stop at the first encountered key colon.
With ansible 2.9.5 from pip3, I get:
    "ansible_facts": {
       
"my_list2": [
           
{
               
"key-1": "$.*?/|\\^(){}+@[]&_-",
               
"key-2": [
                   
"$.*?/|\\^(){}+@[]&_-",
                   
"{ }",
                   
"value23"
               
],
               
"key-3": "value31",
               
"key-4": [
                   
"value41",
                   
"value42"
               
],
               
"key-5": "value51"
           
},
           
{
               
"_key-2": [
                   
"value24",
                   
"value25"
               
],
               
"key--3": "$.*?/|\\^(){}+@[]&_-",
               
"key-1-": "value12",
               
"key-5---": "value52"
           
}
       
]
   
},

 Also, in reality, the input data can be structured in other ways than a list, such as:
my_struct:
  config
:
    key_1: '$.*?/|\^(){}+@[]&_-'
    key_2
:
     
- '$.*?/|\^(){}+@[]&_-'
     
- "{\_}"
     
- value23

We cannot assume that the passed data are a list or not.
Reply all
Reply to author
Forward
0 new messages