The ansible.builtin.copy module in Ansible is used to copy files from the control machine to remote hosts. Here's how it works:
Basic Syntax
- name: Copy a file to the remote host
ansible.builtin.copy:
src: /path/to/local/file
dest: /path/to/remote/destination
Parameters
- src: The path to the file on the control machine that you want to copy.
- dest: The path on the remote host where the file should be copied.
- mode: (Optional) Set the permissions of the file on the remote host.
- owner: (Optional) Specify the owner of the file on the remote host.
- group: (Optional) Specify the group of the file on the remote host.
- content: (Optional) Instead of copying a file, you can directly specify the content to be written to the destination file.
Example
Here’s a simple example of using the copy module in a playbook:
- hosts: webservers
tasks:
- name: Copy a configuration file
ansible.builtin.copy:
src: /local/path/to/config.conf
dest: /remote/path/to/config.conf
mode: '0644'
How It Works
- When the playbook runs, Ansible transfers the specified file from the control machine to the remote host.
- It sets the permissions and ownership based on the parameters provided.
- If the destination file already exists, it will be overwritten unless you specify
force: no.
This module is useful for deploying configuration files, scripts, or any other files needed on remote servers. If you have more questions or need further examples, feel free to ask!
