function is to replace all ‘_’ by ‘-’ in all keys only (and not values) within a list of dictionaries in memory
uses character classes which are probably not supported by regex_replace:
[:blank:]: space and tab
[:space:]: tab, newline, vertical tab, form feed, carriage return, and space
a label and loop
`
sed -E ‘:l; s/^([[:blank:]](-[[:blank:]])?[[1]:]*)([[2]:]*:)/\1-\3/; tl;’
`
I need to use the previous sed over a yaml list of dictionaries.
If I use a shell command (such as printf “%s\n” “{{ var }}” | sed …), the resulting structure is not yaml anymore.
I could save the var into a file, perform the function over the file and reopen the resulting file into memory.
However, I’m looking for a more efficient way, that is perform only memory operation(s).
Is there a way to translate the previous sed expression into a regex_replace command?
Or maybe there is another more ansible way to perform the same function?
The following sed command:
- function is to replace all '_' by '-' in all keys only (and not values) within a list of dictionaries in memory
- uses character classes which are probably not supported by regex_replace:
+ [:blank:]: space and tab
+ [:space:]: tab, newline, vertical tab, form feed, carriage return, and space
- a label and loop
>
sed -E ':l; s/^([[:blank:]]*(-[[:blank:]]*)?[^[:space:]:_]*)_([^[:space:]:]*:)/\1-\3/; tl;'
>
I need to use the previous sed over a yaml list of dictionaries.
If I use a shell command (such as printf "%s\n" "{{ var }}" | sed ...), the resulting structure is not yaml anymore.
I could save the var into a file, perform the function over the file and reopen the resulting file into memory.
However, I'm looking for a more efficient way, that is perform only memory operation(s).
Is there a way to translate the previous sed expression into a regex_replace command?
Or maybe there is another more ansible way to perform the same function?
Start with "{{ var | from_yaml }}" and you get a Python object which can be traversed and manipulated.