ansible / ansible.builtin / v2.6.0 / module / copy Copies files to remote locations | "added in version" historical of ansible.builtin" Authors: Ansible Core Team, Michael DeHaan stableinterface | supported by coreansible.builtin.copy (v2.6.0) — module
pip
Install with pip install ansible==2.6.0
The C(copy) module copies a file from the local or remote machine to a location on the remote machine. Use the M(fetch) module to copy files from remote locations to the local box. If you need variable interpolation in copied files, use the M(template) module.
For Windows targets, use the M(win_copy) module instead.
- name: example copying file with owner and permissions copy: src: /srv/myfiles/foo.conf dest: /etc/foo.conf owner: foo group: foo mode: 0644
- name: The same example as above, but using a symbolic mode equivalent to 0644 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 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 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 copy: src: /mine/sudoers dest: /etc/sudoers validate: /usr/sbin/visudo -cf %s
- name: Copy a "sudoers" file on the remote machine for editing copy: src: /etc/sudoers dest: /etc/sudoers.edit remote_src: yes validate: /usr/sbin/visudo -cf %s
- name: Copy using the 'content' for inline data copy: content: '# This file was moved to /etc/other.conf' dest: /etc/mine.conf'
src: description: - Local path to a file to copy to the remote server; can be absolute or relative. If path is a directory, it is copied recursively. In this case, if path ends with "/", only inside contents of that directory are copied to destination. Otherwise, if it does not end with "/", the directory itself with all contents is copied. This behavior is similar to Rsync. dest: description: - 'Remote absolute path where the file should be copied to. If I(src) is a directory, this must be a directory too. If I(dest) is a nonexistent path and if either I(dest) ends with "/" or I(src) is a directory, I(dest) is created. If I(src) and I(dest) are files, the parent directory of I(dest) isn''t created: the task fails if it doesn''t already exist.' required: true mode: description: - Mode the file or directory should be. For those used to I(/usr/bin/chmod) remember that modes are actually octal numbers. You must either specify the leading zero so that Ansible's YAML parser knows it is an octal number (like C(0644) or C(01777)) or quote it (like C('644') or C('0644') so Ansible receives a string and can do its own conversion from string into number. Giving Ansible a number without following one of these rules will end up with a decimal number which will have unexpected results. As of version 1.8, the mode may be specified as a symbolic mode (for example, C(u+rwx) or C(u=rw,g=r,o=r)). As of version 2.3, the mode may also be the special string C(preserve). C(preserve) means that the file will be given the same permissions as the source file. type: raw force: aliases: - thirsty default: 'yes' description: - the default is C(yes), which will replace the remote file when contents are different than the source. If C(no), the file will only be transferred if the destination does not exist. type: bool version_added: '1.1' version_added_collection: ansible.builtin group: description: - Name of the group that should own the filesystem object, as would be fed to I(chown). - 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. type: str owner: description: - Name of the user that should own the filesystem object, as would be fed to I(chown). - 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. type: str backup: default: 'no' description: - Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly. type: bool version_added: '0.7' version_added_collection: ansible.builtin follow: default: 'no' description: - This flag indicates that filesystem links in the destination, if they exist, should be followed. type: bool version_added: '1.8' version_added_collection: ansible.builtin serole: description: - The role part of the SELinux filesystem object context. - When set to V(_default), it will use the C(role) portion of the policy if available. type: str setype: description: - The type part of the SELinux filesystem object context. - When set to V(_default), it will use the C(type) portion of the policy if available. type: str seuser: description: - The user part of the SELinux filesystem object context. - By default it uses the V(system) policy, where applicable. - When set to V(_default), it will use the C(user) portion of the policy if available. type: str content: description: - When used instead of I(src), sets the contents of a file directly to the specified value. For anything advanced or with formatting also look at the template module. version_added: '1.1' version_added_collection: ansible.builtin decrypt: default: true description: - This option controls the autodecryption of source files using vault. type: bool version_added: '2.4' version_added_collection: ansible.builtin selevel: description: - The level part of the SELinux filesystem object context. - This is the MLS/MCS attribute, sometimes known as the C(range). - When set to V(_default), it will use the C(level) portion of the policy if available. type: str checksum: description: - 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. version_added: '2.5' version_added_collection: ansible.builtin validate: description: - 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 '%s' which must be present as in the examples below. - 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 R(handling complex validation,complex_configuration_validation). type: str attributes: aliases: - attr description: - The attributes the resulting filesystem object should have. - To get supported flags look at the man page for I(chattr) on the target system. - This string should contain the attributes in the same order as the one displayed by I(lsattr). - The C(=) operator is assumed as default, otherwise C(+) or C(-) operators need to be included in the string. type: str version_added: '2.3' version_added_collection: ansible.builtin remote_src: default: 'no' description: - If C(no), it will search for I(src) at originating/master machine. - If C(yes) it will go to the remote/target machine for the I(src). Default is C(no). - Currently I(remote_src) does not support recursive copying. - I(remote_src) only works with C(mode=preserve) as of version 2.6. type: bool version_added: '2.0' version_added_collection: ansible.builtin local_follow: default: 'yes' description: - This flag indicates that filesystem links in the source tree, if they exist, should be followed. type: bool version_added: '2.4' version_added_collection: ansible.builtin unsafe_writes: default: false description: - 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. type: bool version_added: '2.2' version_added_collection: ansible.builtin directory_mode: description: - When doing a recursive copy set the mode for the directories. If this is not set we will use the system defaults. The mode is only set on directories which are newly created, and will not affect those that already existed. version_added: '1.5' version_added_collection: ansible.builtin
backup_file: description: name of backup file created returned: changed and if backup=yes sample: /path/to/file.txt.2015-02-12@22:09~ type: string checksum: description: sha1 checksum of the file after running copy returned: success sample: 6e642bb8dd5c2e027bf21dd923337cbb4214f827 type: string dest: description: destination file/path returned: success sample: /path/to/file.txt type: string gid: description: group id of the file, after execution returned: success sample: 100 type: int group: description: group of the file, after execution returned: success sample: httpd type: string md5sum: description: md5 checksum of the file after running copy returned: when supported sample: 2a5aeecc61dc98c4d780b14b330e3282 type: string mode: description: permissions of the target, after execution returned: success sample: 420 type: string owner: description: owner of the file, after execution returned: success sample: httpd type: string size: description: size of the target, after execution returned: success sample: 1220 type: int src: description: source file used for the copy on the target machine returned: changed sample: /home/httpd/.ansible/tmp/ansible-tmp-1423796390.97-147729857856000/source type: string state: description: state of the target, after execution returned: success sample: file type: string uid: description: owner id of the file, after execution returned: success sample: 100 type: int