Find module questions

486 views
Skip to first unread message

Reji

unread,
Jan 16, 2017, 11:51:33 AM1/16/17
to Ansible Project
I am using Ansible 2.2 and I am running into issues when using the "find" module. My intention is to check for a pattern in a remote host file. Please note the code:

- name: Checking if string exists
  find:
    paths:     '/home/rnair/test.txt'
    contains:  "dev1d111"
  register:    output
  become:      yes
  become_user: root

- debug:
    var: output

OUTPUT:

ok: [xxxx.xxx.com] => {"changed": false, "examined": 0, "files": [], "matched": 0, "msg": "/home/rnair/test.txt was skipped as it does not seem to be a valid directory or it cannot be accessed\n"}

TASK [par : debug] *************************************************************
ok: [
xxxx.xxx.com] => {
    "output": {
        "changed": false,
        "examined": 0,
        "files": [],
        "matched": 0,
        "msg": "/home/rnair/test.txt was skipped as it does not seem to be a valid directory or it cannot be accessed\n"
    }
}

Any help would be much appreciated.


Brian Coca

unread,
Jan 16, 2017, 12:10:24 PM1/16/17
to ansible...@googlegroups.com
paths is for directories to search, not the exact file name. Try this:

- find:
    paths: '/home/rnair'
    pattern: 'test.txt'
    contains: 'dev1d111'


--
----------
Brian Coca

Reji

unread,
Jan 17, 2017, 10:27:40 AM1/17/17
to Ansible Project
Brian,

Thanks for your help. Your suggestion worked. However, I am having trouble with the pattern in my contain. My file content is

jdbc:oracle:thin:@dev1d111.xxxxxxx.com:1521/AAAA.xxxxxx.com

I have tried various combinations for my contain parameter and I still cannot get a matched file. Please note my definition:

- name: Checking if database URL needs to be changed

  find:
    paths:     '/home/rnair'
    pattern:   'test.txt'
    contains:  "*dev1d111*"
    use_regex: true
  register:    result

  become:      yes
  become_user: root

- debug:
    var: result

OUTPUT

TASK [par : debug] *************************************************************
ok: [xxxxx.xx.com] => {
    "result": {
        "changed": false,
        "examined": 10,

        "files": [],
        "matched": 0,
        "msg": ""
    }
}


Any idea what is missing?

Thanks in advance



----------------------------

Brian Coca

unread,
Jan 17, 2017, 2:31:52 PM1/17/17
to ansible...@googlegroups.com
it needs to be a valid regular expression, which your example is not. Try this contains:  ".*dev1d111.*"


----------
Brian Coca

Reji

unread,
Jan 18, 2017, 7:14:38 AM1/18/17
to Ansible Project
Brian,

Thanks for your help. It works. Much appreciated.


-------------------

On Monday, January 16, 2017 at 11:51:33 AM UTC-5, Reji wrote:

Bhargava Sagar C

unread,
Sep 13, 2017, 1:21:41 PM9/13/17
to Ansible Project
Hi,

How can i provide multiple regex patterns in contains attribute and patterns attribute.
Also how can i pass multiple directories  to paths.

Appreciate any help.

Brian Coca

unread,
Sep 14, 2017, 3:15:24 PM9/14/17
to Ansible Project
Use a loop?

...
contains: "{{item}}"
...

with_items:
- pattern1
- patten2
...

--------------
Brian Coca
Reply all
Reply to author
Forward
0 new messages