warn on walking through pathing issues (#74514)

* warn show pathing issues when wallking through provided paths

  moved issues from msg to actual warnings AND a specific return field

  fixes #25314

Co-authored-by: Sam Doran <sdoran@redhat.com>
This commit is contained in:
Brian Coca 2021-05-06 16:14:33 -04:00 committed by GitHub
parent 5463cbb841
commit 35ff4ea95b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 8 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- find does not ignore errors from os.walk anymore and issues warnings as expected.

View file

@ -220,6 +220,12 @@ examined:
returned: success returned: success
type: int type: int
sample: 34 sample: 34
skipped_paths:
description: skipped paths and reasons they were skipped
returned: success
type: dict
sample: {"/laskdfj": "'/laskdfj' is not a directory"}
version_added: '2.12'
''' '''
import fnmatch import fnmatch
@ -372,6 +378,10 @@ def statinfo(st):
} }
def handle_walk_errors(e):
raise e
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
@ -407,6 +417,7 @@ def main():
params['patterns'] = ['*'] params['patterns'] = ['*']
filelist = [] filelist = []
skipped = {}
if params['age'] is None: if params['age'] is None:
age = None age = None
@ -431,15 +442,16 @@ def main():
module.fail_json(size=params['size'], msg="failed to process size") module.fail_json(size=params['size'], msg="failed to process size")
now = time.time() now = time.time()
msg = '' msg = 'All paths examined'
looked = 0 looked = 0
has_warnings = False
for npath in params['paths']: for npath in params['paths']:
npath = os.path.expanduser(os.path.expandvars(npath)) npath = os.path.expanduser(os.path.expandvars(npath))
try: try:
if not os.path.isdir(npath): if not os.path.isdir(npath):
raise Exception("'%s' is not a directory" % to_native(npath)) raise Exception("'%s' is not a directory" % to_native(npath))
for root, dirs, files in os.walk(npath, followlinks=params['follow']): for root, dirs, files in os.walk(npath, onerror=handle_walk_errors, followlinks=params['follow']):
looked = looked + len(files) + len(dirs) looked = looked + len(files) + len(dirs)
for fsobj in (files + dirs): for fsobj in (files + dirs):
fsname = os.path.normpath(os.path.join(root, fsobj)) fsname = os.path.normpath(os.path.join(root, fsobj))
@ -456,7 +468,9 @@ def main():
try: try:
st = os.lstat(fsname) st = os.lstat(fsname)
except (IOError, OSError) as e: except (IOError, OSError) as e:
msg += "Skipped entry '%s' due to this access issue: %s\n" % (fsname, to_text(e)) module.warn("Skipped entry '%s' due to this access issue: %s\n" % (fsname, to_text(e)))
skipped[fsname] = to_text(e)
has_warnings = True
continue continue
r = {'path': fsname} r = {'path': fsname}
@ -498,12 +512,14 @@ def main():
if not params['recurse']: if not params['recurse']:
break break
except Exception as e: except Exception as e:
warn = "Skipped '%s' path due to this access issue: %s\n" % (npath, to_text(e)) module.warn("Skipped '%s' path due to this access issue: %s\n" % (npath, to_text(e)))
module.warn(warn) skipped[npath] = to_text(e)
msg += warn has_warnings = True
if has_warnings:
msg = 'Not all paths examined, check warnings for details'
matched = len(filelist) matched = len(filelist)
module.exit_json(files=filelist, changed=False, msg=msg, matched=matched, examined=looked) module.exit_json(files=filelist, changed=False, msg=msg, matched=matched, examined=looked, skipped_paths=skipped)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -207,7 +207,8 @@
- assert: - assert:
that: that:
- failed_path.files == [] - failed_path.files == []
- failed_path.msg.startswith("Skipped '{{mypath}}' path due to this access issue") - 'failed_path.msg == "Not all paths examined, check warnings for details"'
- mypath in failed_path.skipped_paths
- name: test number of examined directories/files - name: test number of examined directories/files
block: block: