Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How to use regex_replace for backslash and double quotes in ansible playbook

$
0
0

In my ansible (2.15.x) project there is a customer parsed string (as extra-vars). This string is later passed to a python script in a win_shell command (The ansible host is a windows machine). So before parsing I want escape all the single backslashes and double quotes

- name: Prepare the string  set_fact:    modified_string: "{{ string1 | regex_replace('\"','\\\"') | regex_replace('\\','\\\\') }}"- name: Run python script with parameter  win_shell: "python.exe --% --modifiedString \"{{ modified_string }}\" "

But the first task fails with,

{"changed": false,"_ansible_no_log": false,"exception": "Traceback (most recent call last):\n  File \"/usr/lib/python3.9/site-packages/ansible/executor/task_executor.py\", line 505, in _execute\n    self._task.post_validate(templar=templar)\n  File \"/usr/lib/python3.9/site-packages/ansible/playbook/task.py\", line 291, in post_validate\n    super(Task, self).post_validate(templar)\n  File \"/usr/lib/python3.9/site-packages/ansible/playbook/base.py\", line 546, in post_validate\n    value = templar.template(getattr(self, name))\n  File \"/usr/lib/python3.9/site-packages/ansible/template/__init__.py\", line 750, in template\n    d[k] = self.template(\n  File \"/usr/lib/python3.9/site-packages/ansible/template/__init__.py\", line 724, in template\n    result = self.do_template(\n  File \"/usr/lib/python3.9/site-packages/ansible/template/__init__.py\", line 949, in do_template\n    data = _escape_backslashes(data, myenv)\n  File \"/usr/lib/python3.9/site-packages/ansible/template/__init__.py\", line 138, in _escape_backslashes\n    for token in jinja_env.lex(d2):\n  File \"/usr/lib/python3.9/site-packages/jinja2/lexer.py\", line 864, in tokeniter\n    raise TemplateSyntaxError(\njinja2.exceptions.TemplateSyntaxError: unexpected char '\\\\' at 96\n  line 1\n"}

How can I fix this?


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>