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

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

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 }}”

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

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.

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