# regex in command

**URL:** <https://forum.ansible.com/t/regex-in-command/25748>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [October 14, 2017, 6:57am UTC](https://forum.ansible.com/t/regex-in-command/25748 "2017-10-14T06:57:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [October 14, 2017, 6:57am UTC](https://forum.ansible.com/t/regex-in-command/25748/1 "2017-10-14T06:57:27Z")

</div>

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.

```auto
- 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, 

```

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 14, 2017, 2:34pm UTC](https://forum.ansible.com/t/regex-in-command/25748/2 "2017-10-14T14:34:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [October 14, 2017, 4:50pm UTC](https://forum.ansible.com/t/regex-in-command/25748/3 "2017-10-14T16:50:58Z")

</div>

I tried Shell module too like below but it actually serach for vm\*.log file instead of all the vm.log files.

```auto
- 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

```

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 14, 2017, 5:05pm UTC](https://forum.ansible.com/t/regex-in-command/25748/4 "2017-10-14T17:05:14Z")

</div>

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.
