shell module find -exec chown - how to parse/escape {} \; ?

63 views
Skip to first unread message

Joanna Delaporte

unread,
Jul 6, 2016, 12:49:19 PM7/6/16
to Ansible Project
I want to find and chown about 4000 users' files while migrating to a new authentication protocol and domain.

I have a task that looks like this: 
  - name: find and fix UID ownerships in all staff homedirs
    shell
: "find {{'~'+item.name | expanduser }} -uid {{item.uid}} -exec chown {{item.name}} {} \;"
    with_items
:
   
- "{{staff|default([])}}"
   
- "{{students|default([])}}"
   
- "{{staffstuaccounts|default([])}}"
   
when: item.uid is defined


And I get this syntax error:
  - name: find and fix UID ownerships in all staff homedirs
    shell
: "find {{'~'+item.name | expanduser }} -uid {{item.uid}} -exec chown {{item.name}} {} \;"
                                                                                                 
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes
.  Always quote template expression brackets when they
start a value
.

I would use built-in modules, but find doesn't find owners. Stat (as far as I can tell) doesn't go recursive. I'm a little nervous about registering output from find and then writing a new file task to chown the results (which would have to use with_together, I think), in case the user list order gets out of sync between two tasks. Is there a way to escape this shell expression so it will work?

Thanks!
Joanna

Joanna Delaporte

unread,
Jul 6, 2016, 2:25:56 PM7/6/16
to Ansible Project
I figured it out. I also switched to batch mode instead of single mode:

shell: "find /home/staff -gid {{item.0.stdout}} -exec chgrp {{item.1.stdout}} \\{\\} +"
Reply all
Reply to author
Forward
0 new messages