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

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?

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.

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

  - 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

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.