In this article, you will see how we can create, install and remove and copy directories from one local to the remote host and set attributes like permission and ownership and different characteristics through copy module.
1.. About Copy Module
2. Quick Syntax for Playbook and ad-hoc command
3. Copy directories – Local to Remote
- Copy the directory’s content recursively with an example
- 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 command with option. You can read about copy through the below command.
# ansible-doc -l copy
1. About Copy Module
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 copy module.
Now We will try to give most of the expected examples for copy module. Below syntax of ad-hoc and playbook example, some content string is copying into test file in opt directory. further, we will test through copy file to the remote host.
2. Quick Syntax for Playbook and ad-hoc command
Ad-hoc command syntax :
# ansible all -m copy -a 'content="Welcome to Tech Transit \n" dest=/opt/testfile '
You can learn more from the official website Ansible Doc
Using the ansible ad-hoc command 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 copy: src: /home/techtransit/copytest/techdir/ dest: /srv/techtransit
The above playbook is an example of copy-only content of techdir directory, not directory because of trailing / , when we implement trailing slash / then only files will 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 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
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 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 exists. 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.
