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

420 views
Skip to first unread message

shif...@gmail.com

unread,
Jan 18, 2021, 12:46:58 AM1/18/21
to Ansible Project
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`

---
 - name: "Play 3"
   hosts: all
   user: destuser
   tasks:
     - set_fact:
         tdate: "bkp.{{ '%d%b%Y_%H%M%S' | strftime }}"

     - name: Take Backup when dest_path and source path are the same.
       ignore_errors: yes
       copy: 
         src: "/tmp/mohtas/folder"
         dest: "/tmp/mohtas/folder.{{ tdate }}"
         mode: preserve

However, the backup folder was created with a different permissions as below:

drwxr-xr-x 3 destuser destuser 17 Jan 15 09:07 /tmp/mohtas/folder.bkp.15Jan2021_090700

I understand that I can use stat module but was looking for a better/quicker solution as i'm dealing with a loop of files/folders.

My ansible version is:
[destuser@desthost /]$ ansible --version ansible 2.4.2.0 config file = /home/destuser/.ansible.cfg configured module search path = [u'/home/destuser/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /bin/ansible python version = 2.7.5

Note: The strange thing is the backup permissions are preserved if i mention 
src: /tmp/mohtas/file.txt as a file and not a directory.


Gerard Weatherby

unread,
Jan 18, 2021, 12:18:12 PM1/18/21
to Ansible Project
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 }}"


Stefan Hornburg (Racke)

unread,
Jan 18, 2021, 12:21:30 PM1/18/21
to ansible...@googlegroups.com
On 1/18/21 6:18 PM, Gerard Weatherby wrote:
> 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 }}"
>
>

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

Regards
Racke

> On Monday, January 18, 2021 at 12:46:58 AM UTC-5 shif...@gmail.com wrote:
>
> 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`
> **//___^
> ---
>  - name: "Play 3"
>    hosts: all
>    user: destuser
>    tasks:
>      - set_fact:
>          tdate: "bkp.{{ '%d%b%Y_%H%M%S' | strftime }}"
>
>      - name: Take Backup when dest_path and source path are the same.
>        ignore_errors: yes
>        copy: 
>          src: "/tmp/mohtas/folder"
>          dest: "/tmp/mohtas/folder.{{ tdate }}"
>          mode: preserve
> **//___^
>
> However, the backup folder was created with a different permissions as below:
>
> drwxr-xr-x 3 destuser destuser 17 Jan 15 09:07 /tmp/mohtas/folder.bkp.15Jan2021_090700
>
> I understand that I can use statmodule but was looking for a better/quicker solution as i'm dealing with a loop of
> files/folders.
> **//___^
> My ansible version is:
> [destuser@desthost /]$ ansible --version ansible 2.4.2.0 config file = /home/destuser/.ansible.cfg configured module
> search path = [u'/home/destuser/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python
> module location = /usr/lib/python2.7/site-packages/ansible executable location = /bin/ansible python version = 2.7.5
> **//___^
> Note: The strange thing is the backup permissions are preserved if i mention 
> src: /tmp/mohtas/file.txtas a file and not a directory.
> **//___^**//___^**//___^**//___^**//___^
>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/b857c2f2-cf3c-4295-95b4-97442dad7d6an%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/b857c2f2-cf3c-4295-95b4-97442dad7d6an%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Weatherby,Gerard

unread,
Jan 18, 2021, 12:35:47 PM1/18/21
to ansible...@googlegroups.com
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.

-- 
Gerard Weatherby | Application Architect
NMRbox | Department of Molecular Biology and Biophysics | UConn Health
263 Farmington Avenue, Farmington, CT 06030-6406
uchc.edu
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/OmXsuyDV_PA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/13b38bbd-d001-773e-a993-1966ecc9ca94%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Jan 18, 2021, 12:40:48 PM1/18/21
to ansible...@googlegroups.com
On 1/18/21 6:35 PM, Weatherby,Gerard wrote:
> 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
Racke

> -- 
> *Gerard Weatherby* | Application Architect
> NMRbox | Department of Molecular Biology and Biophysics | UConn Health
> 263 Farmington Avenue, Farmington, CT 06030-6406
> uchc.edu <http://uchc.edu>
> https://groups.google.com/d/msgid/ansible-project/62dd39aa-0ce9-4b88-958c-b8a313d0e2e1%40Spark
> <https://groups.google.com/d/msgid/ansible-project/62dd39aa-0ce9-4b88-958c-b8a313d0e2e1%40Spark?utm_medium=email&utm_source=footer>.
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages