Search code examples
regexansibleruntime-errorfull-text-searchruntimeexception

Unable to print lines matching string using Ansible regex


I wish to search for all entries of string starting with "SSLFile" or starting with "SSLFile" in a file(httpd.conf) on remote host; register it to a variable and print all the matches found using ansible regex.

Using slurp and shell module i was able to read the remote file contents.

 - name: Slurp certificate entries
  slurp:
    src: "{{ httpd_home }}/conf/httpd.conf"
#    shell: "cat {{ httpd_home }}/conf/httpd.conf"
  register: filecontent

- name: Find certificate entries
  set_fact:
    input: "{{ filecontent['content'] | b64decode }}"

- name: Regex String
  set_fact:
    target: "{{ input | regex_replace('\\sSSLFile.*, '\\1') }}"

All good except the last regex task where I try to search for regex pattern and assign it to variable called "target". It fails right there and gives the below debug -vvvv error message:

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": ""
}

Can you please suggest how can I grab all the lines matching the pattern ?


Solution

  • The target: "{{ input | regex_replace('\\sSSLFile.*, '\\1') }}" line is an attempt to assign the result of a regex replacement using \sSSLFile.* pattern to find matches and a backreference to Capturing group 1 (\1 in the replacement pattern) to a target variable.

    The use of \1 backreference is wrong because the regex pattern, \sSSLFile.*, has no single capturing group that is specified with a pair of unescaped parentheses.

    Use regex_search to extract data:

    target: "{{ input | regex_search('\\sSSLFile.*') }}"
    

    To get all matches, use regex_findall:

    target: "{{ input | regex_findall('\\sSSLFile.*') }}"