# Setting dir vs file permissions with file "recurse=yes"

**URL:** <https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409>\
**Category:** Ansible Project\
**Created:** [June 5, 2014, 3:35pm UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409 "2014-06-05T15:35:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Sokolovsky](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@Paul\_Sokolovsky](https://forum.ansible.com/u/Paul_Sokolovsky)\
**Post date:** [June 5, 2014, 3:35pm UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409/1 "2014-06-05T15:35:41Z")

</div>

Hello,

I just found out that "file" module's "mode" param accepts only octal  
value. That comes as a little surprise, as  
[http://docs.ansible.com/file\_module.html](http://docs.ansible.com/file_module.html) described "mode" as "mode the  
file or directory should be, such as 0644 as would be fed to chmod". I  
can feed "g+w" to chmod, so I kinda expected being able to use it with  
Ansible either.

Anyway, how can I set just some, not all, permission bits of a  
recursive directory hierarchy? The culprit is "x" permission, which  
means completely different things for file vs dir. So, if I use 0775,  
all files will become executable. And if I use 0664, all directories  
would become non-searchable.

To remind, chmod, besides constructs like "g+w" to set just "w" bit for  
groups, has another device to deal with the issue above -  
"X" (capital), which will set "x" bit only for dirs.

Thanks,  
Paul [mailto:pmiscml@gmail.com](mailto:pmiscml@gmail.com)

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [June 5, 2014, 4:10pm UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409/2 "2014-06-05T16:10:08Z")

</div>

Per the documentation:

“recursively set the specified file attributes (applies only to state=directory) (added in Ansible 1.1)”

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [June 5, 2014, 4:49pm UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409/3 "2014-06-05T16:49:15Z")

</div>

This was submitted quite a while ago, perhaps it could be merged?

---

<div class="post-metadata">

**Author:** ![Paul\_Sokolovsky](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@Paul\_Sokolovsky](https://forum.ansible.com/u/Paul_Sokolovsky)\
**Post date:** [June 5, 2014, 5:17pm UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409/4 "2014-06-05T17:17:56Z")

</div>

Hello,

---

<div class="post-metadata">

**Author:** ![Paul\_Sokolovsky](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@Paul\_Sokolovsky](https://forum.ansible.com/u/Paul_Sokolovsky)\
**Post date:** [June 5, 2014, 6:53pm UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409/5 "2014-06-05T18:53:09Z")

</div>

Hello,

> Per the documentation:
> 
> "recursively set the specified file attributes (applies only to  
> state=directory) (added in Ansible 1.1)"
> 
> So the mode specified when using recurse=yes should be the mode you  
> want applied to the directories. If you want more complex permission  
> setups for large directories of files, I would recommend looking at  
> the synchronize module.

And yet:

- name: Set download dir permissions  
&nbsp;&nbsp;file: dest={{www\_dir}} state=directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner=linaro-ci-publish group=bot-publishers  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode=0775  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;recurse=yes  
&nbsp;&nbsp;sudo: yes

Before:  
-rw-rw-r-- 1 www-data www-data 11 Mar 20 2013 HEADER.html

After:  
-rwxrwxr-x 1 linaro-ci-publish bot-publishers 11 Mar 20 2013  
HEADER.html

Bottom line? Ansible docs are nowhere detailed enough to remind even  
Ansible developers how modules actually work, what to say about users  
who need to \*learn\* how it works.

Another issue is that recursive=yes appear to work rather slow (taking  
chmod -R as reference).

---

<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 6, 2014, 12:42pm UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409/6 "2014-06-06T12:42:50Z")

</div>

We welcome improvements via pull request. Just click the edit link on any page of the docs in the upper right for a link to the GitHub page.

If it’s a module, the docs are embedded in the module source.

---

<div class="post-metadata">

**Author:** ![Damien\_Hollis](https://avatars.discourse-cdn.com/v4/letter/d/ee59a6/32.png) [@Damien\_Hollis](https://forum.ansible.com/u/Damien_Hollis)\
**Post date:** [July 15, 2014, 9:36pm UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409/7 "2014-07-15T21:36:09Z")

</div>

Hi Michael,

I’ve been using Puppet for some time to configure our servers and I’m seriously looking at moving to Ansible. In general I like Ansible a lot more than Puppet but this issue seems to be one area that Puppet has a solution for and Ansible doesn’t. Is there a way to make sure that directories get the x permission while files don’t when using the recurse feature? If not, is there a workaround?

Regards,  
Damien

---

<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:** [July 16, 2014, 11:47pm UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409/8 "2014-07-16T23:47:11Z")

</div>

I’m really not interested in tool comparisons here, but Puppet has not done the recursive thing either and actually takes the position you have to spec out every file path along the way.

I worked for them, I remember this, and I agree. We do the same thing.

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [July 17, 2014, 9:40am UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409/9 "2014-07-17T09:40:44Z")

</div>

> I’m really not interested in tool comparisons here, but Puppet has not done the recursive thing either and actually takes the position you have to spec out every file path along the way.

I suspect Damien meant:

file { ‘/target’:  
source =\> ‘file:///source’,  
recurse =\> true,  
mode =\> ‘0644’,  
}

If /source contains a directory hierarchy, then all files contained will be copied to /target with mode ‘0644’ and all directories (including /target itself) with mode ‘0755’.

That can be quite useful.

Cheers,  
Paul

---

<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:** [July 17, 2014, 12:32pm UTC](https://forum.ansible.com/t/setting-dir-vs-file-permissions-with-file-recurse-yes/15409/10 "2014-07-17T12:32:29Z")

</div>

Ansible does have a recursive copy, so I imagine you are requesting a change in the permissions structure?

I’d also recommend the synchronize module versus passing recurse to the copy module. It’s backed by rsync and is much more efficient.
