I’m testing a playbook and I intend to make a SQL query on an Oracle database.
If the query contains the character “$” or is protected “$”, I need them to be treated as protected “$”.
I’m using the regex_replace filter but I’m having difficulty.
It would be something like:
See if this helps. It uses a regex negative lookbehind, only inserting a backslash in front of the dollar sign if there isn’t already one there. You’ll need to adapt it to your use case a little bit.
---
# escape-dollar.yml
- name: Escape dollar signs
hosts: localhost
gather_facts: false
vars:
dollar_string: |
A. Thi$ line contain$ une$caped dollar $igns.
B. Thi\$ line contain\$ e\$caped dollar \$igns.
tasks:
- name: Exec SQL
shell:
cmd: |
printf "%s\n" '{{ dollar_string }}'
printf "%s\n" '{{ dollar_string | regex_replace('(?<!\)(\$)', '\') }}'
register: cdb_out
- name: Output
debug:
msg: "{{ cdb_out.stdout_lines }}"
## TASK [Output] ***************************************
## ok: [localhost] =>
## msg:
## - A. Thi$ line contain$ une$caped dollar $igns.
## - B. Thi\$ line contain\$ e\$caped dollar \$igns.
## - ''
## - A. Thi\$ line contain\$ une\$caped dollar \$igns.
## - B. Thi\$ line contain\$ e\$caped dollar \$igns.