Find all files owned by a particular user

I’m trying to use the find module to search a path and return all files owned by a particular user. I can get the list of all files in the folder, but the data returned has both the path and the pw_name for each file. How can I narrow the search to return only files where pw_name is a certain user?

for instance using selectattr.

"{{ fs.files|selectattr('pw_name', 'equalto', 'some_user')|list }}"

Thank you for that suggestion! Worked perfectly! I appreciate it very much.

Thanks,
Harry

One other question on this: How can I use a variable as the third parameter? I’m hoping to pass in a user name and match that in the check, but I’m getting an error when I run it.

vars:

  • user_to_remove: ‘user1’

The fact is now set as: “{{ fs.files | selectattr(‘pw_name’,‘equalto’,{{ user_to_remove }}) | list }}”

The error is: template error while templating string: expected token ‘:’, got ‘}’

Any ideas?
Harry

https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names

Awesome! Thanks for that. Got it to work using the lookup function:
“{{ fs.files | selectattr(‘pw_name’,‘equalto’,lookup(‘vars’, ‘user_to_remove’)) | list }}”

Thanks again!
Harry

Hii

Yes, that also worked. The use of the brackets in these types of situations always trips me up.

Thanks,
Harry