Find module questions

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.

paths is for directories to search, not the exact file name. Try this:

  • find:
    paths: ‘/home/rnair’
    pattern: ‘test.txt’
    contains: ‘dev1d111’

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

it needs to be a valid regular expression, which your example is not. Try this contains: “.dev1d111.

Brian,

Thanks for your help. It works. Much appreciated.

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.

Use a loop?

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

with_items:
   - pattern1
   - patten2
...