# Unable to print regex registered variable in Ansible

**URL:** <https://forum.ansible.com/t/unable-to-print-regex-registered-variable-in-ansible/31978>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [January 15, 2020, 6:34am UTC](https://forum.ansible.com/t/unable-to-print-regex-registered-variable-in-ansible/31978 "2020-01-15T06:34:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [January 15, 2020, 6:34am UTC](https://forum.ansible.com/t/unable-to-print-regex-registered-variable-in-ansible/31978/1 "2020-01-15T06:34:40Z")

</div>

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](https://docs.ansible.com/ansible/latest/modules/slurp_module.html#slurp-module)

Regards,

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [January 15, 2020, 7:59am UTC](https://forum.ansible.com/t/unable-to-print-regex-registered-variable-in-ansible/31978/2 "2020-01-15T07:59:32Z")

</div>

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”: “”  
> }

```auto

```

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [January 15, 2020, 12:36pm UTC](https://forum.ansible.com/t/unable-to-print-regex-registered-variable-in-ansible/31978/3 "2020-01-15T12:36:55Z")

</div>

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,
