Find all files owned by a particular user

157 views
Skip to first unread message

lift...@gmail.com

unread,
Jul 1, 2021, 3:20:40 PM7/1/21
to Ansible Project
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?

---
- hosts: localhost
  become: yes
  become_method: sudo
  gather_facts: no

  tasks:

  - name: Find all files under /tmp
    find:
      paths: /tmp
      recurse: yes
    register: fs

  - name: Print output
    debug:
      msg: "{{ fs }}"

What gets printed is every single file found, and fs.files has both path and pw_name.  I want to only show any file where pw_name is the user I'm looking for.

Any ideas?

Thanks,
Harry

Dick Visser

unread,
Jul 1, 2021, 5:09:28 PM7/1/21
to ansible...@googlegroups.com
for instance using selectattr.

"{{ fs.files|selectattr('pw_name', 'equalto', 'some_user')|list }}"
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ac36dc28-a5a1-40d2-84af-c8d07384b65cn%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

lift...@gmail.com

unread,
Jul 2, 2021, 8:13:26 AM7/2/21
to Ansible Project
Thank you for that suggestion!  Worked perfectly!  I appreciate it very much.

Thanks,
Harry

lift...@gmail.com

unread,
Jul 2, 2021, 10:17:35 AM7/2/21
to Ansible Project
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

Dick Visser

unread,
Jul 2, 2021, 10:27:19 AM7/2/21
to ansible...@googlegroups.com

lift...@gmail.com

unread,
Jul 2, 2021, 10:33:11 AM7/2/21
to Ansible Project
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

Dick Visser

unread,
Jul 2, 2021, 10:52:50 AM7/2/21
to ansible...@googlegroups.com
Hii


On Fri, 2 Jul 2021 at 16:33, lift...@gmail.com <lift...@gmail.com> wrote:
>
> Awesome! Thanks for that. Got it to work using the lookup function:
> "{{ fs.files | selectattr('pw_name','equalto',lookup('vars', 'user_to_remove')) | list }}"

Probably this will also work:

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

lift...@gmail.com

unread,
Jul 2, 2021, 11:33:25 AM7/2/21
to Ansible Project
Yes, that also worked.  The use of the brackets in these types of situations always trips me up.

Thanks,
Harry

Reply all
Reply to author
Forward
0 new messages