On 8 May 2017 at 20:07, Dick Visser <
dick....@geant.org> wrote:
> 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.
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):
---
- name: Change permissions recursively
hosts: all
become: yes
vars:
path: /var/www/test
modes:
d: '2755'
f: '0644'
tasks:
- name: Change permissions
command: find "{{ path }}" -type "{{ item.key }}" ! -perm "{{
item.value }}" -exec chmod -v "{{ item.value }}" {} \;
with_dict: "{{ modes }}"
register: result
changed_when: result.stdout != ""