Find module with contains parameter not working

861 views
Skip to first unread message

Saranya N

unread,
May 22, 2018, 8:19:51 AM5/22/18
to Ansible Project
I want to list ONLY Filenames dev.xml and prd.xml that contain a word called “vob.fi.ted.com". Im able to list all filenames dev.xml and prd.xml using find module but when I use contains parameter in playbook it doesn’t return those files although they have the string in their contents.



- hosts: test

gather_facts: false

become: yes

become_user: root

tasks:

- name: Ansible find files examples

find:

paths: /opt/common

patterns: "dev.xml,prd.xml"

contains: "vob.fi.ted.com" -à This is not working

recurse: yes

register: files_to_find



- debug:

msg: "{{ files_to_find.files }}"



I get empty for list of files matched like below output.

However when I remove the contains: "vob.fi.ted.com" parameter , I get the list of matched files. All the files have 777 permission also.







OUTPUT



ok: [xxx.ted.com] => {

"changed": false,

"examined": 3408,

"files": [], ===============è File list is empty

"invocation": {

"module_args": {

"age": null,

"age_stamp": "mtime",

"contains": "vob.fi.ted.com",

"file_type": "file",

"follow": false,

"get_checksum": false,

"hidden": false,

"paths": [

"/opt/common"

],

"patterns": [

"dev.xml",

“prd.xml”

],

"recurse": true,

"size": null,

"use_regex": true

}

},

"matched": 0,

"msg": ""

}

Saranya N

unread,
May 22, 2018, 8:21:41 AM5/22/18
to Ansible Project
I want to list ONLY Filenames dev.xml and prd.xml that contain a word called “vob.fi.ted.com". Im able to list all filenames dev.xml and prd.xml using find module but when I use contains parameter in playbook it doesn’t return those files although they have the string in their contents.



- hosts: test

gather_facts: false

become: yes

become_user: root

tasks:

- name: Ansible find files examples

find:

paths: /opt/common

patterns: "dev.xml,prd.xml"

contains: "vob.fi.ted.com"

recurse: yes

register: files_to_find



- debug:

msg: "{{ files_to_find.files }}"



I get empty for list of files matched like below output.

However when I remove the contains: "vob.fi.ted.com" parameter , I get the list of matched files. All the files have 777 permission also.







OUTPUT



ok: [xxx.ted.com] => {

"changed": false,

"examined": 3408,

"files": [],

Kai Stian Olstad

unread,
May 22, 2018, 2:26:53 PM5/22/18
to ansible...@googlegroups.com
On 22.05.2018 14:19, Saranya N wrote:
> I want to list ONLY Filenames dev.xml and prd.xml that contain a word
> called “vob.fi.ted.com". Im able to list all filenames dev.xml and
> prd.xml using find module but when I use contains parameter in
> playbook it doesn’t return those files although they have the string
> in their contents.
>
>
>
> - hosts: test
>
> gather_facts: false
>
> become: yes
>
> become_user: root
>
> tasks:
>
> - name: Ansible find files examples
>
> find:
>
> paths: /opt/common
>
> patterns: "dev.xml,prd.xml"
>
> contains: "vob.fi.ted.com" -à This is not working
>
> recurse: yes
>
> register: files_to_find

contains is regex patterns, with this expression you would only find
file that only contain that string and nothing more.

To make it find files containing that string you need to have a .* on
the start and at the end.
. in regex means any character, since you mean literally . and not any
character you need to escape the . with \

so you contains should be
contains: .*vob\.fi\.ted\.com.*


--
Kai Stian Olstad

Saranya N

unread,
May 23, 2018, 1:29:24 PM5/23/18
to Ansible Project
Thank you so much Kai , that worked .
Reply all
Reply to author
Forward
0 new messages