# Recursively chown'ing a directory

**URL:** <https://forum.ansible.com/t/recursively-chowning-a-directory/11904>\
**Category:** Ansible Project\
**Created:** [September 17, 2012, 6:34pm UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904 "2012-09-17T18:34:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [September 17, 2012, 6:34pm UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/1 "2012-09-17T18:34:38Z")

</div>

Hi All,  
Prior to Ansible 0.7, the file module would recursively chown  
directories, now it doesn't (seems like the right behavior; i was  
surprised when it did recurse, but that's actually what i wanted in  
this case :)). Is there another way I should be doing this besides  
figuring out each directory I want and maybe using with\_items?

thanks!  
matt

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 17, 2012, 11:22pm UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/2 "2012-09-17T23:22:45Z")

</div>

Yeah, we fixed the glitch.

I'd accept a patch for a recurse option that ads the '-R' ... I almost  
thought we already had that but it looks like no.

The hard part is computing 'changed' in that instance, to which point  
I'd probably say returning 'True' all the time is plenty sufficient  
when recurse is set.

---

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [September 18, 2012, 1:25am UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/3 "2012-09-18T01:25:51Z")

</div>

> Yeah, we fixed the glitch.
> 
> I'd accept a patch for a recurse option that ads the '-R' ... I almost  
> thought we already had that but it looks like no.
> 
> The hard part is computing 'changed' in that instance, to which point  
> I'd probably say returning 'True' all the time is plenty sufficient  
> when recurse is set.

I've got a commit that attempts this at  
[https://github.com/mcodd/ansible/commit/2ec3db0643d52859f45c6c8a846fc8864c77e956](https://github.com/mcodd/ansible/commit/2ec3db0643d52859f45c6c8a846fc8864c77e956).  
Apparently Python doesn't have built-in recursion for chmod/chown so  
you have to use os.walk, but when you do that you actually get  
computing "changed" for free because you can just use the existing  
functions in the file module while looping over the files and  
directories (I'm a pretty novice Python programmer, but this bears  
itself out based on a few basic tests I've run since that commit in my  
local environment).

Didn't submit a pull request yet because I'm on the fence about  
whether and/or how to recurse for chmod'ing. Per the commit above, it  
does just what you tell it to do; e.g. if I say recurse=yes and tell  
it mode=0644 then all my files and directories will become 0644. Even  
thought that's what I asked for, I probably didn't mean it (I probably  
meant I wanted all the files to be 644 but that I still wanted the  
execute bit set on my directories). os.walk makes it easy to just  
chmod the files instead of the directories, but that's also probably  
not what everyone wants (and certainly not what everyone would  
expect). Should we just not allow recurse for mode, leave it as-is,  
or... ?

thanks,  
matt

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 18, 2012, 1:37am UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/4 "2012-09-18T01:37:36Z")

</div>

Hmm, I'd be tempted to just add the -R, as the directory tree could be  
/quite/ large, and coreutils is optimized for the task.

I would guess it would want to do files /and/ directories, and the  
recurse would also affect chgrp.

I'd say we could just leave it out though, as executing via shell  
isn't terrible.

---

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [September 18, 2012, 2:01am UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/5 "2012-09-18T02:01:28Z")

</div>

Are you suggesting that we just use os.system and the OS's chmod/chown  
when recurse is set, to avoid potential scaling problems with using  
Python when the directory trees are huge? If so, I agree w/you that  
just executing via shell is a better alternative, and putting  
os.system calls in the file module just for this purpose would just  
clutter it without adding much value. That said, it was kind of neat  
to realize we got the appropriate value for Changed so easily by doing  
this with os.walk... I think there's some value there, but don't know  
enough about Python to understand the scaling limitations or maybe  
other reasons it's not such a great idea.

matt

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 18, 2012, 2:20am UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/6 "2012-09-18T02:20:57Z")

</div>

Yes, basically I'm thinking of what happens when someone decides to  
try it on a large directory tree.

---

<div class="post-metadata">

**Author:** ![Aleksej\_Romanov](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@Aleksej\_Romanov](https://forum.ansible.com/u/Aleksej_Romanov)\
**Post date:** [September 19, 2012, 4:57pm UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/7 "2012-09-19T16:57:17Z")

</div>

I tried.

$ cp -r /usr/ /tmp/ &\>/dev/null  
$ find /tmp/usr | wc -l  
130615  
$ time chgrp -R user /tmp/usr/

real 0m0.372s  
user 0m0.048s  
sys 0m0.320s  
$ time ansible -c local -i /tmp/i 127.0.0.1 -m file -a 'path=/tmp/usr \  
group=user state=directory recurse=yes' &\>/dev/null

real 0m7.696s  
user 0m5.488s  
sys 0m2.192s

So it is much slower, but still only 7 seconds with quite large directory.  
Assuming that one usually would use this for own project's files, not /,  
it should be quick enough.

According permissions for files and dirs, may be it is reasonable to  
have separate parameters for each one, not 'mode' for both, so it will  
be 'permissions\_mode\_for\_files=644 permissions\_mode\_for\_directories=755'

---

<div class="post-metadata">

**Author:** ![senorsmile](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/senorsmile/32/9024_2.png) [@senorsmile](https://forum.ansible.com/u/senorsmile)\
**Post date:** [October 7, 2013, 8:27pm UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/8 "2013-10-07T20:27:46Z")

</div>

I could definitely use this:permissions\_mode\_for\_files=644 permissions\_mode\_for\_directories=755

or something like it. Right now I have to run manual shell with find like this:

find /some/dir/ -type d -exec /bin/chmod 755 {} ;

find /some/dir/ -type f -exec /bin/chmod 644 {} ;

---

<div class="post-metadata">

**Author:** ![Ben\_Hoyt](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@Ben\_Hoyt](https://forum.ansible.com/u/Ben_Hoyt)\
**Post date:** [June 24, 2014, 6:38pm UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/9 "2014-06-24T18:38:42Z")

</div>

Yeah, I’m seeing this same thing – trying to change the owner of a large (existing) directory recursively is taking 10-20 times as long using the “file” module vs just using chown -R, so I’ve opted to use the latter in our playbooks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 24, 2014, 7:17pm UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/10 "2014-06-24T19:17:23Z")

</div>

FYI: you are replying to a mailing list post from last October.

---

<div class="post-metadata">

**Author:** ![Ben\_Hoyt](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@Ben\_Hoyt](https://forum.ansible.com/u/Ben_Hoyt)\
**Post date:** [June 24, 2014, 7:22pm UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/11 "2014-06-24T19:22:22Z")

</div>

Understood – the intention was to bump this up as it’s still an issue with Ansible 1.5.5. -Ben

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 24, 2014, 7:37pm UTC](https://forum.ansible.com/t/recursively-chowning-a-directory/11904/12 "2014-06-24T19:37:13Z")

</div>

See if there’s a ticket filed and file one would be better, we really don’t bump old threads on this list.

I also don’t consider this an issue so much more as a feature request / enhancement idea, where a pull request that makes it happen would probably be ideal.

You can also keep using the chown.
