ansible.builtin.copy module – Copy files to remote locations
Note
This module is part of ansible-core
and included in all Ansible
installations. In most cases, you can use the short
module name
copy
even without specifying the collections keyword.
However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.copy
for easy linking to the
module documentation and to avoid conflicting with other collections that may have
the same module name.
Synopsis
The ansible.builtin.copy module copies a file or a directory structure from the local or remote machine to a location on the remote machine. File system meta-information (permissions, ownership, etc.) may be set, even when the file or directory already exists on the target system. Some meta-information may be copied on request.
Get meta-information with the ansible.builtin.stat module.
Set meta-information with the ansible.builtin.file module.
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 with the
content
parameter produces unpredictable results.For Windows targets, use the ansible.windows.win_copy module instead.
Note
This module has a corresponding action plugin.
Parameters
Parameter |
Comments |
---|---|
The attributes the resulting filesystem object should have. To get supported flags look at the man page for This string should contain the attributes in the same order as the one displayed by The |
|
Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly. Choices:
|
|
SHA1 checksum of the file being transferred. Used to validate that the copy of the file was successful. If this is not provided, ansible will use the local calculated checksum of the src file. |
|
When used instead of Works only when For advanced formatting or if |
|
This option controls the auto-decryption of source files using vault. Choices:
|
|
Remote absolute path where the file should be copied to. If If If If |
|
Set the access permissions of newly created directories to the given mode. Permissions on existing directories do not change. See The target system’s defaults determine permissions when this parameter is not set. |
|
This flag indicates that filesystem links in the destination, if they exist, should be followed. Choices:
|
|
Influence whether the remote file must always be replaced. If If Choices:
|
|
Name of the group that should own the filesystem object, as would be fed to When left unspecified, it uses the current group of the current user unless you are root, in which case it can preserve the previous ownership. |
|
This flag indicates that filesystem links in the source tree, if they exist, should be followed. Choices:
|
|
The permissions of the destination file or directory. For those used to As of Ansible 1.8, the mode may be specified as a symbolic mode (for example, As of Ansible 2.3, the mode may also be the special string
When doing a recursive copy, see also If If Specifying |
|
Name of the user that should own the filesystem object, as would be fed to When left unspecified, it uses the current user unless you are root, in which case it can preserve the previous ownership. Specifying a numeric username will be assumed to be a user ID and not a username. Avoid numeric usernames to avoid this confusion. |
|
Influence whether If If
Auto-decryption of files does not work when Choices:
|
|
The level part of the SELinux filesystem object context. This is the MLS/MCS attribute, sometimes known as the When set to |
|
The role part of the SELinux filesystem object context. When set to |
|
The type part of the SELinux filesystem object context. When set to |
|
The user part of the SELinux filesystem object context. By default it uses the When set to |
|
Local path to a file to copy to the remote server. This can be absolute or relative. If path is a directory, it is copied recursively. In this case, if path ends with |
|
Influence when to use atomic operation to prevent data corruption or inconsistent reads from the target filesystem object. By default this module uses atomic operations to prevent data corruption or inconsistent reads from the target filesystem objects, but sometimes systems are configured or just broken in ways that prevent this. One example is docker mounted filesystem objects, which cannot be updated atomically from inside the container and can only be written in an unsafe manner. This option allows Ansible to fall back to unsafe methods of updating filesystem objects when atomic operations fail (however, it doesn’t force Ansible to perform unsafe writes). IMPORTANT! Unsafe writes are subject to race conditions and can lead to data corruption. Choices:
|
|
The validation command to run before copying the updated file into the final destination. A temporary file path is used to validate, passed in through Also, the command is passed securely so shell features such as expansion and pipes will not work. For an example on how to handle more complex validation than what this option provides, see handling complex validation. |
Attributes
Attribute |
Support |
Description |
---|---|---|
Support: full |
Indicates this has a corresponding action plugin so some parts of the options can be executed on the controller |
|
Support: none |
Supports being used with the |
|
Support: none |
Forces a ‘global’ task that does not execute per host, this bypasses per host templating and serial, throttle and other loop considerations Conditionals will work as if This action will not work normally outside of lockstep strategies |
|
Support: full |
Can run in check_mode and return changed status prediction without modifying target, if not supported the action will be skipped. |
|
Support: full |
Will return details on what has changed (or possibly needs changing in check_mode), when in diff mode |
|
Platform: posix |
Target OS/families that can be operated against |
|
Support: full |
Uses Ansible’s strict file operation functions to ensure proper permissions and avoid data corruption |
|
Support: full |
Can automatically decrypt Ansible vaulted files |
Notes
Note
The ansible.builtin.copy module recursively copy facility does not scale to lots (>hundreds) of files.
See Also
See also
- ansible.builtin.assemble
Assemble configuration files from fragments.
- ansible.builtin.fetch
Fetch files from remote nodes.
- ansible.builtin.file
Manage files and file properties.
- ansible.builtin.template
Template a file out to a target host.
- ansible.posix.synchronize
A wrapper around rsync to make common tasks in your playbooks quick and easy.
- ansible.windows.win_copy
Copies files to remote locations on windows hosts.
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:
Key |
Description |
---|---|
Name of backup file created. Returned: changed and if backup=yes Sample: |
|
SHA1 checksum of the file after running copy. Returned: success Sample: |
|
Destination file/path. Returned: success Sample: |
|
Group id of the file, after execution. Returned: success Sample: |
|
Group of the file, after execution. Returned: success Sample: |
|
MD5 checksum of the file after running copy. Returned: when supported Sample: |
|
Permissions of the target, after execution. Returned: success Sample: |
|
Owner of the file, after execution. Returned: success Sample: |
|
Size of the target, after execution. Returned: success Sample: |
|
Source file used for the copy on the target machine. Returned: changed Sample: |
|
State of the target, after execution. Returned: success Sample: |
|
Owner id of the file, after execution. Returned: success Sample: |