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

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 .

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

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):