Repository: ansible/ansible

Issue:
'find' module doesn't return checksum when file_type=any
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->
According to the documentation at https://docs.ansible.com/ansible/2.6/modules/find_module.html, 
get_checksum -> Set this to true to retrieve a file's sha1 checksum.

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
find module
/usr/lib/python3.6/site-packages/ansible/modules/files/find.py
##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.6 (default, Jul  5 2018, 16:58:52) [GCC 8.1.1 20180531]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
```
[root@pi ~]# uname -a
Linux pi 4.14.54-2-ARCH #1 SMP Thu Jul 12 01:22:12 UTC 2018 armv7l GNU/Linux
```
##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

<!--- Paste example playbooks or commands between quotes below -->
```shell
ansible -m find -a "paths=/tmp file_type=any get_checksum=true" localhost
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
 I expected the checksum to be a key in the returned dict

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
localhost | SUCCESS => {
    "changed": false,
    "examined": <redacted>,
    "files": [
        {
            "atime": 1531591370.106748,
            "ctime": 1531591370.106748,
            "dev": 42,
            "gid": 0,
            "gr_name": "root",
            "inode": <redacted>,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0644",
            "mtime": 1531591370.106748,
            "nlink": 1,
            "path": "/tmp/aa",
            "pw_name": "root",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 9,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
...
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/files/find.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/files/find.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
Update:
Just found out that using 'file_type=file' reolves this,
I would have expected the module to check if it's a regular file even after providing file_type=any,
I still think it's a bug, or the documentation is misleading, leaving issue open.


Resolving patch:
diff --git a/lib/ansible/modules/files/find.py b/lib/ansible/modules/files/find.py
--- a/lib/ansible/modules/files/find.py
+++ b/lib/ansible/modules/files/find.py
@@ -268,7 +268,7 @@ def contentfilter(fsname, pattern):
                 if prog.match(line):
                     return True
 
-    except:
+    except Exception:
         pass
 
     return False
@@ -280,12 +280,12 @@ def statinfo(st):
 
     try:  # user data
         pw_name = pwd.getpwuid(st.st_uid).pw_name
-    except:
+    except Exception:
         pass
 
     try:  # group data
         gr_name = grp.getgrgid(st.st_gid).gr_name
-    except:
+    except Exception:
         pass
 
     return {
@@ -393,7 +393,7 @@ def main():
 
                     try:
                         st = os.lstat(fsname)
-                    except:
+                    except Exception:
                         msg += "%s was skipped as it does not seem to be a valid file or it cannot be accessed\n" % fsname
                         continue
 
@@ -402,6 +402,8 @@ def main():
                         if pfilter(fsobj, params['patterns'], params['excludes'], params['use_regex']) and agefilter(st, now, age, params['age_stamp']):
 
                             r.update(statinfo(st))
+                            if stat.S_ISREG(st.st_mode) and params['get_checksum']:
+                                r['checksum'] = module.sha1(fsname)
                             filelist.append(r)
 
                     elif stat.S_ISDIR(st.st_mode) and params['file_type'] == 'directory':


Test patch: