# Ansible copy module does not preserve permissions of directories but only files

**URL:** <https://forum.ansible.com/t/ansible-copy-module-does-not-preserve-permissions-of-directories-but-only-files/34455>\
**Category:** Ansible Project\
**Created:** [January 18, 2021, 5:46am UTC](https://forum.ansible.com/t/ansible-copy-module-does-not-preserve-permissions-of-directories-but-only-files/34455 "2021-01-18T05:46:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [January 18, 2021, 5:46am UTC](https://forum.ansible.com/t/ansible-copy-module-does-not-preserve-permissions-of-directories-but-only-files/34455/1 "2021-01-18T05:46:58Z")

</div>

The actual folder had 777 permissions as seen below:

drwxrwxrwx 3 destuser destuser 29 Jan 14 08:40 /tmp/mohtas/folder

I took the backup using the below playbook and wanted to preserve the permissions i.e 777 for the backup folder `/tmp/mohtas/folder.bkp.15Jan2021_090700`

---

<div class="post-metadata">

**Author:** ![Gerard\_Weatherby](https://avatars.discourse-cdn.com/v4/letter/g/f17d59/32.png) [@Gerard\_Weatherby](https://forum.ansible.com/u/Gerard_Weatherby)\
**Post date:** [January 18, 2021, 5:18pm UTC](https://forum.ansible.com/t/ansible-copy-module-does-not-preserve-permissions-of-directories-but-only-files/34455/2 "2021-01-18T17:18:12Z")

</div>

Just use shell and copy:

- hosts: all

become: true

tasks:

- name: backup name

set\_fact:

tdate: “bkp.{{ ‘%d%b%Y\_%H%M%S’ | strftime }}”

- name: Take Backup when dest\_path and source path are the same.

shell: cp -ar /tmp/x “/tmp/x.{{ tdate }}”

---

<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 18, 2021, 5:21pm UTC](https://forum.ansible.com/t/ansible-copy-module-does-not-preserve-permissions-of-directories-but-only-files/34455/3 "2021-01-18T17:21:05Z")

</div>

> Just use shell and copy:
> 
> - hosts: all
> 
> become: true
> 
> tasks:
> 
> - name: backup name
> 
> ```
> set\_fact:
> 
> tdate: &quot;bkp\.\{\{ &#39;%d%b%Y\_%H%M%S&#39; | strftime \}\}&quot;
> 
> ```
> 
> - name: Take Backup when dest\_path and source path are the same.
> 
> ```
> shell: cp \-ar /tmp/x &quot;/tmp/x\.\{\{ tdate \}\}&quot;
> 
> ```

There is no sane reason to use the shell module. This will work with the command module  
as well.

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

---

<div class="post-metadata">

**Author:** ![Gerard\_Weatherby](https://avatars.discourse-cdn.com/v4/letter/g/f17d59/32.png) [@Gerard\_Weatherby](https://forum.ansible.com/u/Gerard_Weatherby)\
**Post date:** [January 18, 2021, 5:35pm UTC](https://forum.ansible.com/t/ansible-copy-module-does-not-preserve-permissions-of-directories-but-only-files/34455/4 "2021-01-18T17:35:34Z")

</div>

I don’t recall claiming that I was sane, only that the shell module would work. Plus, it is less work, being only five letters instead of seven.

---

<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 18, 2021, 5:40pm UTC](https://forum.ansible.com/t/ansible-copy-module-does-not-preserve-permissions-of-directories-but-only-files/34455/5 "2021-01-18T17:40:23Z")

</div>

> I don’t recall claiming that I was sane, only that the shell module would work. Plus, it is less work, being only five  
> letters instead of seven.

From the official documentation:

If you want to execute a command securely and predictably, it may be better to use the ansible.builtin.command module  
instead. Best practices when writing playbooks will follow the trend of using ansible.builtin.command unless the  
ansible.builtin.shell module is explicitly required. When running ad-hoc commands, use your best judgement.

On top of that using "command" is more efficient as it doesn't need to go through the shell.

I don't want to comment on "it is less work" clause.

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