Introduction to Ansible copy
The built-in copy module in the ansible is for the Unix operating system, which copies the file or the folder from the source location to the destination location and the source location could be the local ansible server or the remote shared path, and the destination path is the remote Linux nodes, and similarly, for the windows nodes, win_copy module can be used to leverage the copy functionality of the ansible with the various parameters.
How Does the Ansible Copy Module Work?
The copy module in the ansible works for the Linux operating system, and to deal with the copy between windows operating systems; we can use the Win_Copy module.
Like any other ansible module, the copy module also has the prerequisite before utilizing them into the playbook, like the SSH connection should be open on the remote Linux machine and need the username and password that is configured for the ansible.
Similarly, there should be WINRM communication open (HTTPS), and the service account or the proper authentication should be set up on the windows system before running the copy (or win_copy) playbook for those servers.
- Mandatory parameters for Ansible Copy command, which we can use for both Copy and win_copy
- Dest: Destination path for the file or the folder. If the source path is the folder and the destination folder is not provided, it will automatically create that folder, but if the source path is the file and the destination path is not provided as a file, it will throw an error. The destination path should be the absolute relative path.
- There are other optional modules for the Ansible Copy command, but we will take only a few commands that we may need to properly understand this module.
- src: Source path of the files and the folders. This path can be a relative path or the absolute path. If the source path is the directory, then it copies the files and subfolders recursively. If the source path is a dictionary and ends with the /, then it copies the content of the directory otherwise copies the folder with the name to the destination path.
- force: Its default value is Yes, and it updates the file if there are any file changes. If the value is No, then it transfers the files only they don’t exist on the destination path.
- remote_src: If its value is set to yes, the playbook will search src at the remote or the target location, and if set to no (default), then it will search the source at the local machine.
You can check for the other additional parameters on the ansible documentation website, and we have mentioned some additional in the playbook example as well.
https://docs.ansible.com/ansible/2.4/copy_module.html (For Linux based VMs)
https://docs.ansible.com/ansible/2.7/modules/win_copy_module.html (For Windows-based VMs).
Examples of Ansible copy
Given below are the examples of Ansible copy:
Playbook to Copy files from the ansible local server to the destination server.
In this example, we are using the local ansible server to copy files from the source to the destination Linux machines. The below code will copy ansible.cfg file to the remote node.
- name: Copy file to the Linux machines
- name: copy ansible.cfg file to the destination
Once you have the above code ready, run the playbook from the specified path.
ansible@ansibleserver:/etc/ansible/ansible-playbooks$ ansible-playbook CopyUnixFile.yml
If you want to copy the entire folder to the destination, modify the below code. It will copy the myfiles folder to the destination /tmp directory. If the folder doesn’t exist, then it will create a new folder.
If you specify the /tmp/myfiles/, then it will copy only the folder contents. The folder content process is recursive. It will copy subfolders and files.
To copy the files or folders with the different parameters, you can use the below content from the playbook of the copy module task.
- name: copy ansible.cfg file to the destination
The above playbook will copy the myfiles folder to the destination server(s) /tmp location, and we have specified the force to No, so it will copy only files or folders which do not exist on the remote server. Next, the owner specifies the username that will be the owner of the file and group species the group name of the remote local system that will be the owner of those files and folders.
In addition, we have provided the mode 0777 that is used for setting up the files and folder permissions. Leading 0 should be there; otherwise, it will produce an unexpected result. You can also provide symbolic permissions like u+rw,g-wx,o-rwx in the mode property.
If the source is remote, then you can use remote_src: yes in the playbook.
Playbook to copy files or folders to the windows systems.
For the windows systems, we can use the win_copy module, and the playbook will look similar.
- name: Copy file to the Windows machines
- name: copy files to the destination
Please note here, we are using the win_copy module, and the parameters almost remain the same. The source folder would be on the local ansible server, and the destination is the windows C:\temp folder. The force parameter is yes, so it copies only the files that were changed.
Ansible uses a declarative syntax, and it is easy to tell the playbook that what we need with the copy or win_copy module rather than telling how we will configure script to copy the files and folders like PowerShell, Python, or another imperative language.
This is a guide to Ansible copy. Here we discuss How Does the Ansible Copy Module Work along with the output. You may also look at the following articles to learn more –