Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible copy doesn't set file mode correctly

I've got an Ansible script which among many things copy's some files to the server:

  - name: copy vhost basic files to folder
    copy:
      src: "{{ item }}"
      dest: /var/www/vhosts/mmpew/
      mode: 664
      owner: "{{ deploy_user }}"
      group: "{{ deploy_user }}"
    with_fileglob:
      - ../files/vhost/*

Locally on my Macbook the files have the permissions -rw-r--r--, but even though I set the mode in the ansible script to 664, the resulting files on the server have the permissions -r-----rwt.

Why oh why do the resulting files on the server not match either the mode set in the ansible script, or the original permissions from my local filesystem from which they are copied?

I even tried to set the mode correctly using the Ansible file module:

  - name: Make sure the files I just uploaded are chmodded correctly
    file:
      path: /var/www/vhosts/mmpew/{{ item }}
      mode: 644
    with_items:
      - the.txt
      - files.php
      - here.py

but even though I get no errors from Ansible, the file modes are not set correctly.

Could anybody enlighten me as to what is wrong here? All tips are welcome!

like image 384
kramer65 Avatar asked Oct 23 '17 14:10

kramer65


People also ask

Does ansible copy use SCP?

If you want to copy a file from an Ansible Control Master to remote hosts, the COPY (scp) module would be just fine.

Does ansible copy overwrite?

By default, the ansible copy module does a force copy to the destination and overwrites the existing file when present.

How do I copy files from one directory to another in ansible?

You can use the copy module to copy files and folders from the local server to the remote servers, between remote servers(only files), change the permission of the files, etc. You can use the fetch module to copy files from the remote machine to the local machine.


2 Answers

Use mode: 0644

The 0 is necessary.

like image 69
Jack Avatar answered Oct 25 '22 12:10

Jack


You can specify the mode symbolically:

mode: u=rw,g=r,o=r

This is more readable and less error-prone. Symbolic mode is supported by Ansible >= 1.8, according to the documentation.

like image 44
René Pijl Avatar answered Oct 25 '22 12:10

René Pijl