# Change files permissions with loop

**URL:** <https://forum.ansible.com/t/change-files-permissions-with-loop/34383>\
**Category:** Ansible Project\
**Created:** [December 31, 2020, 6:43pm UTC](https://forum.ansible.com/t/change-files-permissions-with-loop/34383 "2020-12-31T18:43:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thuan1](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@Thuan1](https://forum.ansible.com/u/Thuan1)\
**Post date:** [December 31, 2020, 6:43pm UTC](https://forum.ansible.com/t/change-files-permissions-with-loop/34383/1 "2020-12-31T18:43:12Z")

</div>

Hi all,

My playbook is working for single files but not for directories.  
It doesn’t check the folders permissions recursively.

---

<div class="post-metadata">

**Author:** ![Michael\_M1](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Michael\_M1](https://forum.ansible.com/u/Michael_M1)\
**Post date:** [December 31, 2020, 8:53pm UTC](https://forum.ansible.com/t/change-files-permissions-with-loop/34383/2 "2020-12-31T20:53:33Z")

</div>

Hi Thuan,

I’m not sure why you are just trying to assert the permissions rather than enforce them, but why not enforce them with something like:

- name: Change permissions recursively  
hosts: all  
gather\_facts: False  
ignore\_errors: True

vars:  
path:   
modes:  
d: ‘2755’  
f: ‘0640’

tasks:

- name: Change permissions  
command: find “{{ path }}” -type “{{ item.key }}” ! -perm “{{ item.value }}” -exec chmod -f “{{ item.value }}” {} ;  
with\_dict: “{{ modes }}”  
register: result  
changed\_when: result.stdout != “”

---

<div class="post-metadata">

**Author:** ![Thuan1](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@Thuan1](https://forum.ansible.com/u/Thuan1)\
**Post date:** [December 31, 2020, 9:27pm UTC](https://forum.ansible.com/t/change-files-permissions-with-loop/34383/3 "2020-12-31T21:27:52Z")

</div>

Hi,

I want to use the block and rescue format.  
I want to check the file permissions before change it.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 1, 2021, 11:53am UTC](https://forum.ansible.com/t/change-files-permissions-with-loop/34383/4 "2021-01-01T11:53:37Z")

</div>

> Hi all,
> 
> My playbook is working for single files but not for directories.  
> It doesn't check the folders permissions recursively.

I would use the find module, which works recursively and provides similar information  
as the stat module. You also get a single list as you don't need a loop.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Thuan1](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@Thuan1](https://forum.ansible.com/u/Thuan1)\
**Post date:** [January 1, 2021, 4:10pm UTC](https://forum.ansible.com/t/change-files-permissions-with-loop/34383/5 "2021-01-01T16:10:27Z")

</div>

I know I can use the shell command with find to get the result.  
But it doesn’t seem nice as the loop module.

Thanks

---

<div class="post-metadata">

**Author:** ![Thuan1](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@Thuan1](https://forum.ansible.com/u/Thuan1)\
**Post date:** [January 4, 2021, 3:37pm UTC](https://forum.ansible.com/t/change-files-permissions-with-loop/34383/6 "2021-01-04T15:37:26Z")

</div>

Hello,

Can somebody give me some examples of find module for this case ?

---

<div class="post-metadata">

**Author:** ![Thuan](https://avatars.discourse-cdn.com/v4/letter/t/ac8455/32.png) [@Thuan](https://forum.ansible.com/u/Thuan)\
**Post date:** [June 7, 2022, 9:25pm UTC](https://forum.ansible.com/t/change-files-permissions-with-loop/34383/7 "2022-06-07T21:25:31Z")

</div>

Nvm, I found an alternative solution.  
Using find module and loop for this purpose.
