Introduction
, you will explore the Ansible Copy module, which allows you to copy files and directories to remote hosts. The Copy module provides a flexible and efficient way to transfer files as part of your Ansible automation tasks.
Copy a File to Remote Host
In this step, you will copy a file from the control machine to a remote host using the Ansible Copy module.
First, create a new Ansible playbook file called /home/labex/project/copy-module-playbook.yaml and open it in a text editor.
Add the following content to the playbook file:
- hosts: localhost
tasks:
- name: Copy a file to remote host
copy:
src: /home/labex/file.txt
dest: /home/labex/project/file.txt
copy: Utilizes the Ansiblecopymodule, which is used to copy files from the control node (local host) to a remote host.
src: Specifies the source file path, indicating the path of the file to be copied on the local host.
dest: Specifies the destination file path, indicating where the file should be copied to on the remote host.
Through this playbook task, the file /home/labex/file.txt will be copied to the path /home/labex/project/file.txt on the remote host. The Ansible copy module facilitates the process of copying files efficiently.
Next, create a file called file.txt in the /home/labex directory.
echo "This is the content of the file." > /home/labex/file.txt
Then, run the playbook using the following command:
ansible-playbook copy-module-playbook.yaml
Example output:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Copy a file to remote host] **********************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Finally, verify that the file file.txt exists in the specified destination path on the remote host.
cat /home/labex/project/file.txt
Example output:
This is the content of the file.
Here "This is the content of the file." is the same as the content of the /home/labex/file.txt file, which means that the /home/labex/file.txt file has been successfully copied to /home/labex/project/file.txt.
Copy a Directory to Remote Host
In this step, you will copy a directory from the control machine to a remote host using the Ansible Copy module.
First, modify the existing playbook file by removing all content and adding the following content to the playbook file:
- hosts: localhost
tasks:
- name: Copy a directory to remote host
copy:
src: /home/labex/directory
dest: /home/labex/project/
With the step1 type, if src is set to directory, this Playbook task copies the /home/labex/directory directory to the /home/labex/project/ path on the remote host.
Next, create a directory called directory in /home/labex directory and create a file called file2.txt in directory directory.
mkdir /home/labex/directory
touch /home/labex/directory/file2.txt
Then, run the playbook using the following command:
ansible-playbook copy-module-playbook.yaml
Example output:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Copy a directory to remote host] *****************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Finally, verify that the directory directory and its contents exist in the specified destination path on the remote host.
tree /home/labex/project
Expected output:
/home/labex/project
├── copy-module-playbook.yaml
├── directory
│ └── file2.txt
└── file.txt
1 directory, 3 files
You can see that the directory and the file2.txt file have been successfully copied to the /home/labex/project directory on the target host.
Preserve File Attributes
In this step, you will learn how to preserve file attributes, such as permissions and timestamps, when using the Ansible Copy module.
First, modify the existing playbook file by removing all content and adding the following content to the playbook file:
- hosts: localhost
tasks:
- name: Preserve file attributes
copy:
src: /home/labex/file.txt
dest: /tmp/file.txt
mode: preserve
mode: preserve: This parameter preserves the mode (permissions) of the source file during the copy process, ensuring that the destination file retains the same permissions as the source file.
Through this playbook task, the file /home/labex/file.txt will be copied to /tmp/file.txt on the remote host, while preserving the file attributes such as permissions.
Then, run the playbook using the following command:
ansible-playbook copy-module-playbook.yaml
Example output:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Preserve file attributes] ************************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Finally, verify that the file file.txt is copied to the specified destination path on the remote host while preserving its attributes.
Use ll to view /home/labex/file.txt details:
ll /home/labex/file.txt
Example output:
-rw-rw-r-- 1 labex labex 33 Mar 9 08:34 /home/labex/file.txt
Use ll to view /tmp/file.txt details:
ll /tmp/file.txt
Example output:
-rw-rw-r-- 1 labex labex 33 Mar 9 09:00 /tmp/file.txt
This Ansible playbook can be used to preserve permission patterns (permission bits) and owner information during the replication process.
Handle Permissions
In this step, you will explore how to handle file permissions when using the Ansible Copy module. You will learn how to set specific permissions for the copied file on the remote host.
First, modify the existing playbook file by removing all content and adding the following content to the playbook file:
- hosts: localhost
tasks:
- name: Handle permissions
copy:
src: /home/labex/file.txt
dest: /tmp/file1.txt
mode: "0644"
mode: Replace"0644"with the desired permission mode for the file. Refer to the
Want to Learn More?
- 🌳 Learn the latest
- 💬 Join our
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.- 🌳 Learn the latest
SOCIAL SHARE CARD GENERATOR