Trick to remove a backslash liternal in Ansible?

773 views
Skip to first unread message

Jibun no Kage

unread,
Jan 14, 2022, 10:18:30 AM1/14/22
to Ansible Project
New to Ansible, having trouble doing the following:  shell command returns a "This is a test\u0000" string.  I can't seem to find a way to trim the '\u0000'.  Since it seem to be tricking to remove a backslash literal in Ansible?

The following would work in a shell command...
 cat /proc/device-tree/model | cut -d '\' -f1

But Ansible chokes on the '\'?  And even '\\'  does not work since cut command can't handle more than one char.  So the cut parsing trips or Ansible parsing trips.  A bit of a catch-22.

Figure there has to be an Ansible elegant way to do this?

Any help appreciated.

Stefan Hornburg (Racke)

unread,
Jan 14, 2022, 2:49:54 PM1/14/22
to ansible...@googlegroups.com
- debug:
msg: "{{ input | regex_replace('\\\\u0000', '') }}"
vars:
input: 'This is a test\u0000'

You might need *four* backslashes to get rid of the original backslash.

But you can get away with *two" by swapping the quotes:

- debug:
msg: '{{ input | regex_replace("\\u0000", "") }}'
vars:
input: 'This is a test\u0000'

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/a71e1eb3-4ddb-4461-bffd-1a3ea5004e2bn%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/a71e1eb3-4ddb-4461-bffd-1a3ea5004e2bn%40googlegroups.com?utm_medium=email&utm_source=footer>.


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

OpenPGP_signature

Todd Lewis

unread,
Jan 17, 2022, 10:27:23 PM1/17/22
to Ansible Project
You haven't posted any Ansible for us to help you with. Could you include something you think should work but doesn't? Otherwise we're just guessing what you're trying to do, and that rarely works well for anybody.
Reply all
Reply to author
Forward
0 new messages