Feature: add owner and group filter to builtin.find#76768
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
|
This needs a rebase. |
|
@webknjaz rebase is done! 👍🏻 |
|
Outside of the rebase, the Note, I've not performed any review of this PR. |
ah fair! didn't saw that. Removed the function now |
s-hertel
left a comment
There was a problem hiding this comment.
This change would need a minor_changes changelog fragment and test cases added to https://github.com/ansible/ansible/blob/devel/test/integration/targets/find/ ensure it keeps working.
| version_added: "2.17" | ||
| default: [] | ||
| groups: | ||
| description: | ||
| - If set, only elements owned by a group in the list are returned | ||
| type: list | ||
| aliases: [ group ] | ||
| elements: str | ||
| version_added: "2.17" |
There was a problem hiding this comment.
The version_added for the new options should be "2.21" now.
| if (users is None or (iter(users) and len(users) < 1)) and \ | ||
| (groups is None or (iter(groups) and len(groups) < 1)): | ||
| return True | ||
| if (iter(users) and len(users) > 0) or (iter(groups) and len(groups) > 0): | ||
| stinfo = statinfo(st) | ||
| if iter(users) and len(users) > 0: | ||
| if stinfo['pw_name'] not in users: | ||
| return False | ||
| if iter(groups) and len(groups) > 0: | ||
| if stinfo['gr_name'] not in groups: | ||
| return False |
There was a problem hiding this comment.
Calling statinfo for every path more than once feels inefficient. Can we convert the list of users and groups to a list of ints in main() for quick comparison? Then this could be:
| if (users is None or (iter(users) and len(users) < 1)) and \ | |
| (groups is None or (iter(groups) and len(groups) < 1)): | |
| return True | |
| if (iter(users) and len(users) > 0) or (iter(groups) and len(groups) > 0): | |
| stinfo = statinfo(st) | |
| if iter(users) and len(users) > 0: | |
| if stinfo['pw_name'] not in users: | |
| return False | |
| if iter(groups) and len(groups) > 0: | |
| if stinfo['gr_name'] not in groups: | |
| return False | |
| if users and st.st_uid not in users: | |
| return False | |
| if groups and st.st_gid not in groups: | |
| return False |
| users=dict(type='list', default=[], aliases=['user'], elements='str'), | ||
| groups=dict(type='list', default=[], aliases=['group'], elements='str'), |
There was a problem hiding this comment.
find has -uid/-gid flags too. We could support either with something like:
uids = []
for user in params['users']:
try:
uid = int(user)
except ValueError:
try:
uid = pwd.getpwnam(user).pw_uid
except KeyError:
module.fail_json(msg=f'Failed to look up user {user}')
uids.append(uid)
gids = []
for group in params['groups']:
try:
gid = int(group)
except ValueError:
try:
gid = grp.getgrnam(group).gr_gid
except KeyError:
module.fail_json(msg=f'Failed to look up group {group}')
gids.append(gid)| users: | ||
| description: | ||
| - If set, only elements owned by a user in the list are returned | ||
| type: list | ||
| aliases: [ user ] | ||
| elements: str | ||
| version_added: "2.17" | ||
| default: [] |
There was a problem hiding this comment.
| users: | |
| description: | |
| - If set, only elements owned by a user in the list are returned | |
| type: list | |
| aliases: [ user ] | |
| elements: str | |
| version_added: "2.17" | |
| default: [] | |
| users: | |
| description: | |
| - If set, only elements owned by a user in the list are returned. | |
| type: list | |
| elements: str | |
| version_added: "2.21" |
| groups: | ||
| description: | ||
| - If set, only elements owned by a group in the list are returned | ||
| type: list | ||
| aliases: [ group ] | ||
| elements: str | ||
| version_added: "2.17" | ||
| default: [] |
There was a problem hiding this comment.
| groups: | |
| description: | |
| - If set, only elements owned by a group in the list are returned | |
| type: list | |
| aliases: [ group ] | |
| elements: str | |
| version_added: "2.17" | |
| default: [] | |
| groups: | |
| description: | |
| - If set, only elements owned by a group in the list are returned. | |
| type: list | |
| elements: str | |
| version_added: "2.21" |
| - name: find all elements owned by user and group root:shadow | ||
| find: | ||
| paths: /etc | ||
| user: root |
There was a problem hiding this comment.
I would keep the plural form to maintain uniformity.
SUMMARY
Adds new parameters to
builtin.findto be able to filter results by owner and group.See examples below for clarification.
The filters are built in a similar way to the existing age and size filter.
They work on the existing data after the file list is obtained.
The filters work for directories, files and links.
Fixes #76586
ISSUE TYPE
COMPONENT NAME
ansible.builtin.findADDITIONAL INFORMATION
How to Use