# issue faced while changing permission of files and directories using file module in ansible 2.3.0

**URL:** <https://forum.ansible.com/t/issue-faced-while-changing-permission-of-files-and-directories-using-file-module-in-ansible-2-3-0/24414>\
**Category:** Ansible Project\
**Created:** [May 7, 2017, 7:59pm UTC](https://forum.ansible.com/t/issue-faced-while-changing-permission-of-files-and-directories-using-file-module-in-ansible-2-3-0/24414 "2017-05-07T19:59:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vibhav\_vishal\_singh](https://avatars.discourse-cdn.com/v4/letter/v/c4cdca/32.png) [@vibhav\_vishal\_singh](https://forum.ansible.com/u/vibhav_vishal_singh)\
**Post date:** [May 7, 2017, 7:59pm UTC](https://forum.ansible.com/t/issue-faced-while-changing-permission-of-files-and-directories-using-file-module-in-ansible-2-3-0/24414/1 "2017-05-07T19:59:18Z")

</div>

Hi All,

I have been trying to avoid shell and command module to change permissions , I am using file module here .

what I am trying : change permission of all directories inside a directory to 755 and all files to 644 .  
problem is I am not supposed to use shell and command module , so while trying to use file module I face issue .

yaml file is below :

tasks:

- file:  
path: /tmp/apps/abc  
state: directory  
mode: 0755  
recurse: yes
- file:  
path: /tmp/apps/abc  
state: file  
mode: 0644

so when running this I get error message saying /tmp/apps/abc is a directory .

but when I put below:

tasks:

- file:  
path: /tmp/apps/abc  
state: directory  
mode: 0755  
recurse: yes
- file:  
path: /tmp/apps/abc  
state: file  
mode: 0644  
recurse: yes

it gives an error saying recurse can be used only with directory .

now I am not sure how to proceed .

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 8, 2017, 6:07pm UTC](https://forum.ansible.com/t/issue-faced-while-changing-permission-of-files-and-directories-using-file-module-in-ansible-2-3-0/24414/2 "2017-05-08T18:07:46Z")

</div>

You can't use the file module to recursively set permissions on files.  
Instead you could first use the find module, and then use file with with\_items.  
Note that this can be quite slow as every file will be taken into account.  
I haven't found a way to instruct 'find' to only find files with (or  
lacking) a specific mode.

Dick

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 10, 2017, 11:24am UTC](https://forum.ansible.com/t/issue-faced-while-changing-permission-of-files-and-directories-using-file-module-in-ansible-2-3-0/24414/3 "2017-05-10T11:24:10Z")

</div>

Actually, the file module does set permissions recursively using the  
'recurse' option, but when you use this the same permission get  
applied to both directories and files, which IMHO isn't very useful.  
I tried the use the find module, register the results, and then change  
any file/dir that needs changing, but as I suspected this is extremely  
slow for any serious directory structure.  
So in the end the command way did turn out the be the easiest and fastest...

Abusing chmod's verbose flag you can use it as an indicator of change  
and have idempotency (optionally define more file types, see man find  
for a list of them):
