I'm not sure how much sense it would be to set directories to 0775 (rwxrwxr-x
) and files to 0644 (rw-r--r--
): group-writeable directories but not files?
If you meant to set files to 0664 (rw-rw-r--
) to ensure that files are not executable while directories are traversable then there is an elegant solution involving only one chmod
command:
chmod -c -R ug=rw,o=r,a-x+X "{{top_dir}}"
Here is how it can be used in Ansible:
- name: recursive chmod example
command: |
chmod -c -R ug=rw,o=r,a-x+X "{{item}}"
register: chmod_status
changed_when: chmod_status.stdout != ""
with_items:
- "/home/user/sample/dir"
chmod -c
prints all the changes that we can conveniently use to populate "changed" status in Ansible. I hope it make sense.