[stable-2.16] find: do not fail on PermissionError (#82880)

* Log and skip permission errors on files and directories

Fixes: #82027


(cherry picked from commit f73d72e830)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/83024/head
Abhijeet Kasurde 8 months ago committed by GitHub
parent edbb6d1d59
commit 416517c90e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,3 @@
---
bugfixes:
- find - do not fail on Permission errors (https://github.com/ansible/ansible/issues/82027).

@ -258,6 +258,7 @@ skipped_paths:
version_added: '2.12' version_added: '2.12'
''' '''
import errno
import fnmatch import fnmatch
import grp import grp
import os import os
@ -434,10 +435,6 @@ def statinfo(st):
} }
def handle_walk_errors(e):
raise e
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
@ -482,6 +479,12 @@ def main():
filelist = [] filelist = []
skipped = {} skipped = {}
def handle_walk_errors(e):
if e.errno in (errno.EPERM, errno.EACCES):
skipped[e.filename] = to_text(e)
return
raise e
if params['age'] is None: if params['age'] is None:
age = None age = None
else: else:

@ -1 +1,3 @@
shippable/posix/group1 shippable/posix/group1
destructive
needs/root

@ -1,3 +1,4 @@
dependencies: dependencies:
- prepare_tests - prepare_tests
- setup_test_user
- setup_remote_tmp_dir - setup_remote_tmp_dir

@ -375,5 +375,61 @@
- 'remote_tmp_dir_test ~ "/astest/.hidden.txt" in astest_list' - 'remote_tmp_dir_test ~ "/astest/.hidden.txt" in astest_list'
- '"checksum" in result.files[0]' - '"checksum" in result.files[0]'
# Test permission error is correctly handled by find module
- vars:
test_dir: /tmp/permission_test
block:
- name: Set up content
file:
path: "{{ test_dir }}/{{ item.name }}"
state: "{{ item.state }}"
mode: "{{ item.mode }}"
owner: "{{ item.owner | default(omit) }}"
group: "{{ item.group | default(omit) }}"
loop:
- name: readable
state: directory
owner: "{{ test_user_name }}"
mode: "1711"
- name: readable/1-unreadable
state: directory
mode: "0700"
- name: readable/2-readable
state: touch
owner: "{{ test_user_name }}"
mode: "0777"
- name: Find a file in readable directory
find:
paths: "{{ test_dir }}/readable/"
patterns: "*"
recurse: true
register: permission_issue
become_user: "{{ test_user_name }}"
- name: Find a file in readable directory
find:
paths: "{{ test_dir }}/readable/"
patterns: "*"
recurse: true
register: permission_issue
become_user: "{{ test_user_name }}"
become: yes
- name: Check if the skipped_paths are populated correctly with permission error
assert:
that:
- permission_issue is success
- not permission_issue.changed
- permission_issue.skipped_paths|length == 1
- "'{{ test_dir }}/readable/1-unreadable' in permission_issue.skipped_paths"
- "'Permission denied' in permission_issue.skipped_paths['{{ test_dir }}/readable/1-unreadable']"
- permission_issue.matched == 1
always:
- name: cleanup test directory
file:
dest: "{{ test_dir }}"
state: absent
- name: Run mode tests - name: Run mode tests
import_tasks: mode.yml import_tasks: mode.yml

Loading…
Cancel
Save