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.
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: