How you can use the copy module in an Ansible playbook

Category : Devops | Sub Category : Devops | By Prasad Bonam Last updated: 2023-07-23 14:42:16 Viewed : 331


How you can use the copy module in an Ansible playbook:

In Ansible, you can use the copy module to copy files and directories from the local machine (the control node) to remote hosts. The copy module allows you to manage file transfers during playbook execution. Here is how you can use the copy module in an Ansible playbook:

Example: Copy a file to remote hosts

Lets say you have a local file called example.txt, and you want to copy it to the /tmp directory on remote servers. Create a playbook like this:

yaml
--- - name: Copy directory to remote hosts hosts: your_remote_servers become: yes tasks: - name: Copy a directory to remote host copy: src: /path/to/source_directory # Replace with the path to your local directory dest: /tmp/destination_directory # Destination path on remote host remote_src: yes # Optional, set to yes if src is on the remote host itself

Replace your_remote_servers with the target server(s) where you want to copy the file. You can specify the IP addresses or domain names of your remote servers in this place.

To run the playbook:

bash
ansible-playbook -i your_inventory_file your_playbook.yml

Example: Copy a directory to remote hosts

If you want to copy a directory and its contents to the remote hosts, use the following playbook:

yaml
--- - name: Copy directory to remote hosts hosts: your_remote_servers become: yes tasks: - name: Copy a directory to remote host copy: src: /path/to/source_directory # Replace with the path to your local directory dest: /tmp/destination_directory # Destination path on remote host remote_src: yes # Optional, set to yes if src is on the remote host itself

Replace your_remote_servers with the target server(s) where you want to copy the directory.

To run the playbook:

bash
ansible-playbook -i your_inventory_file your_playbook.yml

Remember to replace /path/to/example.txt, /path/to/source_directory, and /tmp/destination_directory with the actual paths you want to use.

The copy module in Ansible provides various options for managing file permissions, ownership, and more. For detailed information about the copy module and its parameters, refer to the official Ansible documentation: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html


Search
Sub-Categories
Related Articles

Leave a Comment: