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


On Wednesday, January 15, 2020 at 12:04:52 PM UTC+5:30, Jean-Yves LENHOF 
wrote:
>
> 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...@googlegroups.com <javascript:>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/47deda2a-1a8c-4387-9a5b-d2ea548ad752%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/ansible-project/47deda2a-1a8c-4387-9a5b-d2ea548ad752%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/790b6d70-a7bb-4465-b802-982f8fe05366%40googlegroups.com.

Reply via email to