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

Viewed 647

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

---
- name: "Play 3"
  hosts: all
  user: destuser
  gather_facts: false

  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

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

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
0 Answers
Related