Related to file management the commonly used module provided with ansible-core in the ansible.builtin content collection. File management modules perform tasks such as creating, modifying copying, and other attributes. In this post, you will see how we can create, install remove, and copy files & directories from one local to the remote host and set attributes like permission and ownership and different characteristics through the copy module.

Installation of Ansible Follow steps Here

1. About Copy Module ( ansible.builtin.copy )

2. Quick Syntax for Playbook and ad-hoc command

3. Copy directories – Local to Remote

  1. Copy the directory’s content recursively with an example
  2. Copy the directory and its content recursive with the example

4. Copy a file to managed hosts and Set Attributes

The best resource to read about any Ansible is ansible documentation, where you list all modules through the ansible-doc -l or if you are using the latest ansible automation platform 2.10 then ansible-navigator doc -l. you can use a small module name copy instead of using with collection name ansible. builtin but it is recommended to use a fully qualified collection name to link with the module documentation and avoid conflict with other collections. You can read about copy through the below command.

# ansible-doc ansible.builtin.copy 
# ansible-navigator doc ansible.builtin.copy 

You can learn more from the official website Ansible Doc

1. About Copy Module ( ansible.builtin.copy )

The copy module copies files from one location to managed host locations, it is similar to the file module. The copy can also set permission and other attributes like ownership, group ownership, links, etc. If would you like to copy files from a remote location to the local system then you will use the fetch module instead of the copy module.

Now, let us delve into several anticipated examples pertinent to the copy module. First, we will outline the syntax for both ad-hoc commands and playbook examples. These demonstrations will illustrate the process of copying designated content strings into a ‘test’ file within the ‘opt’ directory. Subsequently, we will proceed to validate the functionality by executing a test scenario involving the transfer of the file to the remote host.

2. Quick Syntax for Playbook and ad-hoc command

Ad-hoc command syntax with ansible.builtin.copy :

# ansible all -m ansible.builtin.copy  -a 'content="Welcome to Tech Transit \n" dest=/opt/testfile '

Using the ansible ad-hoc command ansible.builtin.copy create a test file inside the /opt directory and add the content string “Welcome to Tech Transit \n”.

Output:


srv1.techtransit.org | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "8bbb8b62fd12b14f0ee73a813e50b348f25226a3",
"dest": "/opt/testfile",
"gid": 0,
"group": "root",
"md5sum": "488908a815822c1fee0df0fbe81334c3",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 25,
"src": "/home/techtransit/.ansible/tmp/ansible-tmp-1663502937.4805746-1244-152772706161681/source",
"state": "file",
"uid": 0
}

The value in “changed”: true, which means the file in opt is if not created then it will create and add text provided through command.

3 Copy directories – Local to Remote :


1. Copy the directory’s content recursively with an example :

 cat directorycontent.yml 
---
- name: Copy the directory’s content recursively with an example  
  hosts: all
  tasks:
    - name: Copy  techdir content in /srv/techtransit to remote host
      ansible.builin.copy:
        src:  /home/techtransit/copytest/techdir/
        dest: /srv/techtransit 

The above playbook is an example of copy-only content of techdir directory, not the directory because of trailing /, when we implement trailing slash / then only files will copy.

ansible.builtin.copy

2. Copy the directory and its content recursive with the example :


Now, the directory and content both will be copied to the remote server.

Below is the playbook to copy a directory to the remote server.

 ---
- name: Copy the directory’s content recursively with an example  
  hosts: all
  tasks:
    - name: Copy  techdir content in /srv/techtransit to remote host
      ansible.builtin.copy:
        src:  /home/techtransit/copytest/techdir
        dest: /srv/techtransit

Now trailing slash has been remote from the old example. Now it should copy the directory to /srv/techtransit .

4. Copy a file to managed hosts and Set Attributes thorugh ansible.builtin.copy

Below is the playbook sample which copies the local files to the remote-managed host.

---
- name: Copy file without forcefully
  hosts: all
  tasks:
    - name: Copy  srctest.txt as srcdest.txt in the /opt to remote host
      ansible.builtin.copy:
        src:  /home/techtransit/copytest/srctest.txt
        dest: /opt/srcdest.txt
        force: no 

We set no force argument, So this will not copy if the file will already exist. Now above playbook will copy the srctest.txt file from the/home/techtransit location and copy it to all managed hosts into /opt destination with the name srcdest.txt.

The Ansible Copy Module simplifies the task of replicating files and directories across diverse infrastructure environments.

By Sachin G

I am a professional freelance contributor and founder of tech transit. Love to write and lover of education, culture, and community. I have been using it, setting, supporting, and maintaining it since 2009.Linux rocks! Sachin G Follow me on LinkedIn and x formerly twitter