In this step, you will learn how to use one of the most fundamental Ansible modules: ansible.builtin.copy. This module is used to transfer files from your control node (the LabEx VM) to a specified location on your managed hosts. In our case, the managed host will be localhost itself. Beyond just copying, the copy module allows you to precisely control the file's attributes, such as its owner, group, and permission mode, which is essential for proper system configuration.
First, let's set up our project environment. All our work will be done inside the ~/project directory.
-
Navigate to the project directory and create a subdirectory for our source files. This is a common practice to keep your project organized.
Install the ansible-core package.
sudo dnf install -y ansible-core
Then, navigate to the project directory and create a subdirectory for our source files.
cd ~/project
mkdir files
-
Next, create a simple text file that we will copy. We'll use a cat command with a "here document" to create the file info.txt inside the files directory.
cat << EOF > ~/project/files/info.txt
This file was deployed by Ansible.
It contains important system information.
EOF
-
Now, create an Ansible inventory file. The inventory tells Ansible which hosts to manage. For this lab, we will manage the local machine. Create a file named inventory.ini.
cat << EOF > ~/project/inventory.ini
localhost ansible_connection=local
EOF
In this inventory, localhost is the host we are targeting. The variable ansible_connection=local instructs Ansible to execute the tasks directly on the control node, without using SSH.
-
Create your first Ansible playbook. This playbook will contain the instructions to copy the file. Use nano or cat to create a file named copy_file.yml.
nano ~/project/copy_file.yml
Add the following content to the file. This playbook defines one task: to copy info.txt to the /tmp/ directory and set its attributes.
---
- name: Deploy a static file to localhost
hosts: localhost
tasks:
- name: Copy info.txt and set attributes
ansible.builtin.copy:
src: files/info.txt
dest: /tmp/info.txt
owner: labex
group: labex
mode: "0640"
Let's break down the parameters in the copy task:
src: files/info.txt: The path to the source file on the control node, relative to the playbook's location.
dest: /tmp/info.txt: The absolute path where the file will be placed on the managed host.
owner: labex: Sets the file's owner to the labex user.
group: labex: Sets the file's group to the labex group.
mode: '0640': Sets the file's permissions. 0640 means the owner can read/write, the group can read, and others have no permissions.
-
Execute the playbook using the ansible-playbook command. The -i flag specifies our inventory file.
ansible-playbook -i inventory.ini copy_file.yml
You should see output indicating the successful execution of the playbook, similar to this:
PLAY [Deploy a static file to localhost] ***************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Copy info.txt and set 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 was copied correctly and has the right attributes. Use the ls -l command to check the permissions, owner, and group.
ls -l /tmp/info.txt
The output should show that labex is the owner and group, and the permissions are -rw-r-----.
-rw-r----- 1 labex labex 72 Jul 10 14:30 /tmp/info.txt
You can also view the file's content to ensure it was copied completely.
cat /tmp/info.txt
This file was deployed by Ansible.
It contains important system information.