regex in command

51 views
Skip to first unread message

Vijay Misra

unread,
Oct 14, 2017, 2:57:27 AM10/14/17
to Ansible Project
Hi All ,

I have to search for a pattern string in the various log files located in some directory  on the host. how we can use regex kind of a thing with command module or may be some other useful ansible way i am not aware about.

here is my code which complains  about vm*.log.

- name: Check whether vmware log file contains TDR
command: grep 'TDR_DUMP' '{{guest_vm_path}}/{{item}}/vm*.log'
with_items:
- '{{guest_list}}'
- '{{guest_name_poweredon}}'
ignore_errors: True
register: output

- debug:
var: output

- debug: msg="No TDR signature found"
when: output["results"][0].rc == 1 and
output["results"][1].rc == 1 and
output["results"][2].rc == 1

- debug: msg="Found the TDR signature"
when: output["results"][0].rc == 0 or
output["results"][1].rc == 0 or
output["results"][2].rc == 0


- debug:
var: output.stdout_lines
when: output["results"][0].rc == 0 or
output["results"][1].rc == 0 or
output["results"][2].rc == 0

OutPUT:

ok: [x.x.x.x] => {
"output": {
"changed": true,
"failed": true,
"msg": "One or more items failed",
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": [
"grep",
"TDR_DUMP",
"/vmfs/volumes/mag27_datastore/test1/vm*.log"
],
"delta": "0:00:00.015495",
"end": "2017-10-14 04:35:30.309261",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "grep 'TDR_DUMP' '/vmfs/volumes/mag27_datastore/test1/vm*.log'",
"_uses_shell": false,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
}
},
"item": "test1",
"rc": 2,
"start": "2017-10-14 04:35:30.293766",
"stderr": "grep: /vmfs/volumes/mag27_datastore/test1/vm*.log: No such file or directory",
"stderr_lines": [
"grep: /vmfs/volumes/mag27_datastore/test1/vm*.log: No such file or directory"
],
"stdout": "",
"stdout_lines": []
},
{
"_ansible_item_result": true,

Kai Stian Olstad

unread,
Oct 14, 2017, 10:35:10 AM10/14/17
to ansible...@googlegroups.com
On 14. okt. 2017 08:57, Vijay Misra wrote:
> I have to search for a pattern string in the various log files located in
> some directory on the host. how we can use regex kind of a thing with
> command module or may be some other useful ansible way i am not aware about.
>
> here is my code which complains about vm*.log.

command module is not running a shell so shell expansion like vm*.log
will not work, you need to use the shell module.

You could also do this with the find module.


--
Kai Stian Olstad

Vijay Misra

unread,
Oct 14, 2017, 12:50:58 PM10/14/17
to Ansible Project
 I tried Shell module too like below but it actually serach for vm*.log file instead of all the vm.log files.

- name: Check whether vmware log file contains TDR
  shell: grep 'TDR_DUMP' '{{guest_vm_path}}/{{item}}/vmware*.log'

with_items:
- '{{guest_list}}'
- '{{guest_name_poweredon}}'
ignore_errors: True
register: output

 TASK [ESX_TDR_DUMP : debug] *******************************************************************************************************************
ok: [xxxxx] => {

"output": {
"changed": true,
"failed": true,
"msg": "One or more items failed",
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
                "cmd": "grep 'TDR_DUMP' '/vmfs/volumes/mag27_datastore/test1/vmware*.log'", 
"delta": "0:00:00.021120",
"end": "2017-10-14 07:45:12.942906",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "grep 'TDR_DUMP' '/vmfs/volumes/mag27_datastore/test1/vmware*.log'",
"_uses_shell": true,

"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
}
},
"item": "test1",
"rc": 2,
                "start": "2017-10-14 07:45:12.921786", 
"stderr": "grep: /vmfs/volumes/mag27_datastore/test1/vmware*.log: No such file or directory",
"stderr_lines": [
"grep: /vmfs/volumes/mag27_datastore/test1/vmware*.log: No such file or directory"

],
"stdout": "",
"stdout_lines": []
},
{
"_ansible_item_result": true,
                "_ansible_no_log": false, 
"_ansible_parsed": true,
"changed": true,
                "cmd": "grep 'TDR_DUMP' '/vmfs/volumes/mag27_datastore/test2/vmware*.log'", 
"delta": "0:00:01.024557",
"end": "2017-10-14 07:45:14.937439",
"failed": true,
"invocation": {
-VM

Kai Stian Olstad

unread,
Oct 14, 2017, 1:05:28 PM10/14/17
to ansible...@googlegroups.com
On 14. okt. 2017 18:50, Vijay Misra wrote:
> I tried Shell module too like below but it actually serach for vm*.log
> file instead of all the vm.log files.
>
> - name: Check whether vmware log file contains TDR
> shell: grep 'TDR_DUMP' '{{guest_vm_path}}/{{item}}/vmware*.log'


Not an Ansible issue, but you are using single quotes around the file names.
Single qoutes mean _don't do_ expansion, aka send it unchanged to the
command, so you need to loose them or replace them with double quotes.

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