How to translate a complex sed expressions into regex_replace

23 views
Skip to first unread message

jean-christophe manciot

unread,
Feb 25, 2020, 9:48:52 AM2/25/20
to Ansible Project
ansible 2.9.5 (pip3)

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?

Stefan Hornburg (Racke)

unread,
Feb 25, 2020, 10:01:33 AM2/25/20
to ansible...@googlegroups.com
Start with "{{ var | from_yaml }}" and you get a Python object which can be traversed and manipulated.

Regards
Racke

>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/dfd15e15-8b8d-4601-8b11-b3a69a529fdb%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/dfd15e15-8b8d-4601-8b11-b3a69a529fdb%40googlegroups.com?utm_medium=email&utm_source=footer>.


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

signature.asc

jean-christophe manciot

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

{{ var }} is already a yaml object: it is the list of dictionaries.

Stefan Hornburg (Racke)

unread,
Feb 25, 2020, 10:37:15 AM2/25/20
to ansible...@googlegroups.com
On 2/25/20 4:27 PM, jean-christophe manciot wrote:
>
> {{ var }} is already a yaml object: it is the list of dictionaries.

Good .. so there is no point to using shell + sed here, right?

Loop over the list of dictionaries and manipulate it with "set_fact".

Maybe you can give an example how a dictionary looks before and after the conversion?

Regards
Racke

>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/0e5d0f81-815d-4e8f-bf44-f5750aae08aa%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/0e5d0f81-815d-4e8f-bf44-f5750aae08aa%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc
Reply all
Reply to author
Forward
0 new messages