Escaping a single backslash in the "replace" part of a regex_replace

2,206 views
Skip to first unread message

akuma d

unread,
Mar 9, 2017, 3:01:05 AM3/9/17
to Ansible Project
Hi,
I need to replace a dot in a string with a "\!". I am using "regex_replace" but am unable to get the regex to print a single backslash.

I have tried escaping it using double-backslash but that gives me 2 backslashes in the output. Interestingly escaping using 4 backslashes also gives 2 backslashes in the output - so could this be a bug?

Ansible version is 2.2.1.0


---
- name: install MQ 8
  hosts
: 192.168.90.84
  vars
:
    qmgr
: PAAA.QMGR
  tasks
:
   
- debug:
        msg
: "{{ ini }}"
      vars
:
        ini
: "{{ qmgr | regex_replace('[.]','\\!') }}"



Here is the output:

TASK [debug] *******************************************************************
ok
: [192.168.90.84] => {
   
"msg": "PAAA\\!QMGR"
}


Thanks

Dick Visser

unread,
Mar 9, 2017, 2:25:24 PM3/9/17
to ansible...@googlegroups.com
Your regex is fine, and there is really only one backslash in the output.
It's the escaping of debug that is fooling you.
Try adding this task and check the content:

- copy:
dest: /tmp/test
content: "{{ ini }}"

Dick

On 8 March 2017 at 12:40, 'akuma d' via Ansible Project
> --
> 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.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/6308c90f-8ac5-42d0-9ba6-ed8ba4841eda%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Dick Visser
Sr. System & Network Engineer
GÉANT

Want to join us? We're hiring: https://www.geant.org/jobs
Reply all
Reply to author
Forward
0 new messages