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

145 views
Skip to first unread message

vibhav vishal singh

unread,
May 8, 2017, 7:14:40 AM5/8/17
to Ansible Project
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 .

Dick Visser

unread,
May 8, 2017, 2:08:13 PM5/8/17
to ansible...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/186c34c3-50c9-4689-808a-11df1c92e51f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Dick Visser
Sr. System & Network Engineer
GÉANT

Want to join us? We're hiring: https://www.geant.org/jobs

Dick Visser

unread,
May 10, 2017, 7:24:42 AM5/10/17
to ansible...@googlegroups.com
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 != ""
Reply all
Reply to author
Forward
0 new messages