Re: [ansible-project] Unable to print regex registered variable in Ansible

381 views
Skip to first unread message

Jean-Yves LENHOF

unread,
Jan 15, 2020, 1:34:52 AM1/15/20
to ansible...@googlegroups.com

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,


Le 15/01/2020 à 06:34, Shifa Shaikh a écrit :
I wish to search for all entries of string starting with "SSLFile" or starting with "<whitespache>SSLFile" in a file(httpd.conf) and register it to a variable and print all the matches found. 

The string is found as evident from the output and the file is not modified which is good; but I'm unable to print (debug) it. I get error as I try to print. Below is my playbook:

    - name: Find entries
      lineinfile
:
        path
: "/tmp/httpd.conf"
        regexp
: "\\sSSLFile.*"
        state
: absent
      check_mode
: yes
      changed_when
: false
     
register: filedet

    - debug:
        msg: "{{ filedet }}"

   
- debug:
        msg
: "{{ item.split()[1] }}"
      with_items
:
       
- "{{ filedet.stdout_lines }}"

I get the below error when i run the playbook:


ok: [10.9.9.11] => {
"backup": "",
"changed": false,
"diff": [
{
"after": "",
"after_header": "/tmp/httpd.conf (content)",
"before": "",
"before_header": "/tmp/httpd.conf (content)"
},
{
"after_header": "/tmp/httpd.conf (file attributes)",
"before_header": "/tmp/httpd.conf (file attributes)"
}
],
"found": 1,
"invocation": {
"module_args": {
"attributes": null,
"backrefs": false,
"backup": false,
"content": null,
"create": false,
"delimiter": null,
"directory_mode": null,
"firstmatch": false,
"follow": false,
"force": null,
"group": null,
"insertafter": null,
"insertbefore": null,
"line": null,
"mode": null,
"owner": null,
"path": "/tmp/httpd.conf",
"regexp": "\\sSSLFile.*",
"remote_src": null,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"state": "absent",
"unsafe_writes": null,
"validate": null
}
},
"msg": "1 line(s) removed"
} TASK [debug] *******************************************************************
task path: /app/test.yml:924
ok: [10.9.9.11] => {
"msg": {
"backup": "",
"changed": false,
"diff": [
{
"after": "",
"after_header": "/tmp/httpd.conf (content)",
"before": "",
"before_header": "/tmp/httpd.conf (content)"
},
{
"after_header": "/tmp/httpd.conf (file attributes)",
"before_header": "/tmp/httpd.conf (file attributes)"
}
],
"failed": false,
"found": 1,
"msg": "1 line(s) removed"
}
} TASK [debug] *******************************************************************
task path: /app/test.yml:928
fatal: [10.9.9.11]: FAILED! => {
"msg": "'dict object' has no attribute 'stdout_lines'"
}

          
Can you please suggest what is the correct way to print all the searched matched strings without modifying the file ? I wish to use the the registered variable to perform other actions later in the playbook.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/47deda2a-1a8c-4387-9a5b-d2ea548ad752%40googlegroups.com.

Shifa Shaikh

unread,
Jan 15, 2020, 2:59:32 AM1/15/20
to Ansible Project
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": ""
}
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dick Visser

unread,
Jan 15, 2020, 6:06:01 AM1/15/20
to ansible...@googlegroups.com
On Wed, 15 Jan 2020 at 06:35, Shifa Shaikh <shif...@gmail.com> wrote:
>
> I wish to search for all entries of string starting with "SSLFile" or starting with "<whitespache>SSLFile" in a file(httpd.conf) and register it to a variable and print all the matches found.

It's not entirely clear what you mean by this.
Can you post an example httpd.conf, and what you want to get out of it?

Jean-Yves LENHOF

unread,
Jan 15, 2020, 7:37:39 AM1/15/20
to Shifa Shaikh, Ansible Project
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,
--
Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté.

Kai Stian Olstad

unread,
Jan 15, 2020, 9:52:12 AM1/15/20
to ansible...@googlegroups.com
On 15.01.2020 06:34, Shifa Shaikh wrote:
> I wish to search for all entries of string starting with "SSLFile" or
> starting with "<whitespache>SSLFile" in a file(httpd.conf) and register it
> to a variable and print all the matches found.
>
> The string is found as evident from the output and the file is not modified
> which is good; but I'm unable to print (debug) it. I get error as I try to
> print. Below is my playbook:
>
> - name: Find entries
> lineinfile:
> path: "/tmp/httpd.conf"
> regexp: "\\sSSLFile.*"
> state: absent
> check_mode: yes
> changed_when: false
> register: filedet
>
> - debug:
> msg: "{{ filedet }}"
>
> - debug:
> msg: "{{ item.split()[1] }}"
> with_items:
> - "{{ filedet.stdout_lines }}"
>
> Can you please suggest what is the correct way to print all the searched
> matched strings without modifying the file ? I wish to use the the
> registered variable to perform other actions later in the playbook.

Just use grep

- command: grep -P "^\s*SSLFILE" /tmp/httpd.conf
register: results

- debug: msg="{{ results.stdout }}"

- debug: msg="{{ item }}"
with_items: "{{ results.stdout_lines }}"


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages