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

2020-01-15 Thread Kai Stian Olstad

On 15.01.2020 06:34, Shifa Shaikh wrote:

I wish to search for all entries of string starting with "SSLFile" or
starting with "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

--
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/35f8142c-79b6-9e73-4df6-cb07462af366%40olstad.com.


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

2020-01-15 Thread Jean-Yves LENHOF
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, 

Le 15 janvier 2020 08:59:32 GMT+01:00, Shifa Shaikh  a 
écrit :
>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 "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] 
>>> 

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

2020-01-15 Thread Dick Visser
On Wed, 15 Jan 2020 at 06:35, Shifa Shaikh  wrote:
>
> I wish to search for all entries of string starting with "SSLFile" or 
> starting with "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?

-- 
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/CAL8fbwOBLGAbfgGRNVX%2B10vpOrRnv3f6RA%2BWPZGKRSiC03gjew%40mail.gmail.com.


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

2020-01-15 Thread Shifa Shaikh
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 "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) 

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

2020-01-14 Thread Jean-Yves LENHOF

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"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:Findentries
      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-project+unsubscr...@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 
.


--
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/82efc325-730b-bd86-9a11-8ead9a5d8713%40lenhof.eu.org.


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

2020-01-14 Thread Shifa Shaikh
I wish to search for all entries of string starting with "SSLFile" or 
starting with "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-project+unsubscr...@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.