# How to print all matched strings using Ansible regex

**URL:** <https://forum.ansible.com/t/how-to-print-all-matched-strings-using-ansible-regex/31977>\
**Category:** Ansible Project\
**Created:** [January 15, 2020, 6:07am UTC](https://forum.ansible.com/t/how-to-print-all-matched-strings-using-ansible-regex/31977 "2020-01-15T06:07:03Z")\
**Posts on this page:** 1\
**Page:** 1

<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, 6:07am UTC](https://forum.ansible.com/t/how-to-print-all-matched-strings-using-ansible-regex/31977/1 "2020-01-15T06:07:03Z")

</div>

I wish to match all lines that begin with SSLFile or SSLFile in a file httpd.conf on a remote server and store the matched found strings in a variable for use later in the playbook.

Below is what i did.

`

- name: Find entries  
shell: “cat /tmp/httpd.conf”  
register: filecontent

- name: Debug filecontentvar  
debug:  
msg: “{{ filecontent }}”

- name: Find certificate entries  
set\_fact:  
target: “{{ filecontent.stdout | regex\_replace('\sSSLFile.\*, ‘\1’) }}”

`

Using debug verbose I was able to confirm that the filecontent variable gets the contents of the httpd.conf file of the remote host.

However, the variable called “target” does not get matched lines for searched string despite it being present and errors as below:

> TASK [Find certificate entries] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> **task path: /app/test.yml:906**  
> 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 64  
> line 1 fatal: [10.9.9.11]: FAILED! =\> {  
> “msg”: “Unexpected failure during module execution.”,  
> “stdout”: “”  
> }

I used [https://regex101.com](https://regex101.com/) to confirm that my \sSSLFile.\* condition is good and returns matches on my httpd.conf file.

Can you please suggest ?

```auto

```
