ansible.builtin.copy – Copy files to remote locations¶
Note
This module is part of ansible-base
and included in all Ansible
installations. In most cases, you can use the short module name
copy even without specifying the collections:
keyword.
Despite that, we recommend you use the FQCN for easy linking to the module
documentation and to avoid conflicting with other collections that may have
the same module name.
Synopsis¶
The
copy
module copies a file from the local or remote machine to a location on the remote machine.Use the ansible.builtin.fetch module to copy files from remote locations to the local box.
If you need variable interpolation in copied files, use the ansible.builtin.template module. Using a variable in the
content
field will result in unpredictable output.For Windows targets, use the ansible.windows.win_copy module instead.
Note
This module has a corresponding action plugin.
Parameters¶
Notes¶
Note
The ansible.builtin.copy module recursively copy facility does not scale to lots (>hundreds) of files.
Supports
check_mode
.
See Also¶
See also
- ansible.builtin.assemble
The official documentation on the ansible.builtin.assemble module.
- ansible.builtin.fetch
The official documentation on the ansible.builtin.fetch module.
- ansible.builtin.file
The official documentation on the ansible.builtin.file module.
- ansible.builtin.template
The official documentation on the ansible.builtin.template module.
- ansible.posix.synchronize
The official documentation on the ansible.posix.synchronize module.
- ansible.windows.win_copy
The official documentation on the ansible.windows.win_copy module.
Examples¶
- name: Copy file with owner and permissions
ansible.builtin.copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: '0644'
- name: Copy file with owner and permission, using symbolic representation
ansible.builtin.copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: u=rw,g=r,o=r
- name: Another symbolic mode example, adding some permissions and removing others
ansible.builtin.copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: u+rw,g-wx,o-rwx
- name: Copy a new "ntp.conf" file into place, backing up the original if it differs from the copied version
ansible.builtin.copy:
src: /mine/ntp.conf
dest: /etc/ntp.conf
owner: root
group: root
mode: '0644'
backup: yes
- name: Copy a new "sudoers" file into place, after passing validation with visudo
ansible.builtin.copy:
src: /mine/sudoers
dest: /etc/sudoers
validate: /usr/sbin/visudo -csf %s
- name: Copy a "sudoers" file on the remote machine for editing
ansible.builtin.copy:
src: /etc/sudoers
dest: /etc/sudoers.edit
remote_src: yes
validate: /usr/sbin/visudo -csf %s
- name: Copy using inline content
ansible.builtin.copy:
content: '# This file was moved to /etc/other.conf'
dest: /etc/mine.conf
- name: If follow=yes, /path/to/file will be overwritten by contents of foo.conf
ansible.builtin.copy:
src: /etc/foo.conf
dest: /path/to/link # link to /path/to/file
follow: yes
- name: If follow=no, /path/to/link will become a file and be overwritten by contents of foo.conf
ansible.builtin.copy:
src: /etc/foo.conf
dest: /path/to/link # link to /path/to/file
follow: no
Return Values¶
Common return values are documented here, the following are the fields unique to this module:
Authors¶
Ansible Core Team
Michael DeHaan