Ashar
Ashar

Reputation: 3075

Ansible find module fails to print files found matching pattern

My target server home directory has files like below:

$ cd ~
$ ls -la
total 84
drwxr--r--   7 wluser wluser  4096 Jun 11 02:56 .
drwxr-xr-x. 32 root    root     4096 Jun 10 03:20 ..
drwx------   4 wluser wluser    25 May 26 12:17 .ansible
-rw-------   1 wluser wluser 34681 Jun 11 03:29 .bash_history
-rw-r--r--   1 wluser wluser    18 Aug 21  2019 .bash_logout
-rw-r--r--   1 wluser wluser   193 Aug 21  2019 .bash_profile
-rw-r--r--   1 wluser wluser   231 Aug 21  2019 .bashrc
drwxrwxr-x   4 wluser wluser    27 Feb 17 04:48 .cache
drwxrwxr-x   4 wluser wluser    28 Feb 17 08:17 .config
-rw-r--r--   1 wluser wluser   172 Feb 17  2020 .kshrc
-rw-------   1 wluser wluser    40 May 28 11:59 .lesshst
drwxr-----   3 wluser wluser    18 Feb 17 08:17 .pki
-rw-------   1 wluser wluser  1204 Jul  5 10:05 .sh_history
drwxr--r--   2 wluser wluser    76 May 28 10:59 .ssh
-rw-------   1 wluser wluser 13314 Jun 11 02:56 .viminfo

I use the below ansible play to find all file matching pattern:

121         - find:
122             paths: "/home/wluser/"
123             recurse: no
124             file_type: any
125             patterns:
126               - "*.sh*"
127               - "*.bash*"
128               - "*.ksh*"
129               - "*.profile*"
130               - ".ssh"
131           register: to_copy
132
133
134         - debug:
135             msg: "FOUNDDDXX {{ to_copy}}"

The output shows that it did not match anything when i expected this to work and display all the file in the home directory.

TASK [find] ***************************************************************************************************
task path: /web/playbooks/ansibleuser/va_action.yml:121

ok: [remotehost1] => {
    "changed": false,
    "examined": 72,
    "files": [],
    "invocation": {
        "module_args": {
            "age": null,
            "age_stamp": "mtime",
            "contains": null,
            "file_type": "any",
            "follow": false,
            "get_checksum": false,
            "hidden": false,
            "paths": [
                "/home/wluser/"
            ],
            "patterns": [
                "*.sh*",
                "*.bash*",
                "*.ksh*",
                "*.profile*",
                ".ssh"
            ],
            "recurse": false,
            "size": null,
            "use_regex": false
        }
    },
    "matched": 0,
    "msg": ""
}

TASK [debug] **************************************************************************************************
task path: /web/playbooks/ansibleuser/va_action.yml:134
ok: [remotehost1] => {
    "msg": "FOUNDDDXX {u'files': [], u'changed': False, 'failed': False, u'examined': 72, u'msg': u'', u'matched': 0}"
}

The pattern matching may be incorrect but the .ssh folder should have matched for sure however, that too is not found.

$ ansible --version
ansible 2.4.2.0
  config file = /home/ansbladm/.ansible.cfg
  python version = 2.7.5

Can you please suggest how can i display all files under the home directory matching the patterns ?

Upvotes: 0

Views: 1254

Answers (1)

JBone
JBone

Reputation: 1836

This works for me

tasks:
- name: find files
  find:
    paths: /home/myuserId
    patterns:
    - "(.*bash.*)$"
    - "(.*ssh.*)$"
    use_regex: yes
    hidden: yes
  register: list_of_files
- name: print files
  debug:
    msg: "{{ list_of_files.files }}"

Note that use_regex: true and hidden: true. Without that argument, It did not work for me as well. I see you missed that argument in your code. Try that and let me know what you see

Upvotes: 1

Related Questions