Unable to print regex registered variable in Ansible

Hi,

Perhaps you should better use slurp module to register the content of the file and do some regexp to print what you want on it…

https://docs.ansible.com/ansible/latest/modules/slurp_module.html#slurp-module

Regards,

Reading the contents of the file is not the challenge. I used both sllurp as well as cat and I can see the file contents in the debug. The error occurs when I regex for the desired string.

`

  • name: Slurp certificate entries
    slurp:
    src: “{{ httpd_home }}/conf/httpd.conf”
    register: filecontent

  • name: Find certificate entries
    set_fact:
    input: “{{ filecontent[‘content’] | b64decode }}”

  • debug:
    msg: “{{ input }}”

  • name: Regex String
    set_fact:
    target: “{{ input | regex_replace('\sSSLFile.*, ‘\1’) }}”

`

The regex task fails where we are assigning the set_fact “target” with the below error:

TASK [Regex String] ***************************************
task path: /app/test.yml:908
The full traceback is:
Traceback (most recent call last):
File “/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py”, line 144, in run
res = self._execute()
File “/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py”, line 576, in _execute
self._task.post_validate(templar=templar)
File “/usr/lib/python2.7/site-packages/ansible/playbook/task.py”, line 268, in post_validate
super(Task, self).post_validate(templar)
File “/usr/lib/python2.7/site-packages/ansible/playbook/base.py”, line 384, in post_validate
value = templar.template(getattr(self, name))
File “/usr/lib/python2.7/site-packages/ansible/template/init.py”, line 584, in template
disable_lookups=disable_lookups,
File “/usr/lib/python2.7/site-packages/ansible/template/init.py”, line 539, in template
disable_lookups=disable_lookups,
File “/usr/lib/python2.7/site-packages/ansible/template/init.py”, line 773, in do_template
data = _escape_backslashes(data, myenv)
File “/usr/lib/python2.7/site-packages/ansible/template/init.py”, line 145, in _escape_backslashes
for token in jinja_env.lex(d2):
File “/usr/lib/python2.7/site-packages/jinja2/lexer.py”, line 733, in tokeniter
name, filename)
TemplateSyntaxError: unexpected char u’\’ at 51
line 1 fatal: [10.9.9.11]: FAILED! => {
“msg”: “Unexpected failure during module execution.”,
“stdout”: “”
}


Not undestanding exactly what you want to achieve…

For the syntax error you forgot a quote
So instead of this

target: “{{ input | regex_replace(‘\sSSLFile., ‘\1’) }}"
You need this
target: "{{ input | regex_replace('\sSSLFile.
’ , ‘\1’) }}”

Regards,