In this article, we are going to see how to copy or SCP files and directories from local to remote. Control machine to the remote server.
We are going to learn how to SCP files from local to the remote host
Ansible has dedicated modules to support the copy between local and remote servers in both directions.
The copy module - to copy files from ansible control machine local to the remote machine, host/host group
The fetch module - to copy files from remote host to local (control machine)
In this post, we are going to practice a few examples of copy
module and learn how to SCP files from local to remote in ansible.
If you are looking for a way to copy files between remote servers ( remote server to another remote server ) check out this article
If you are new to Ansible and wondering what is Ansible ad hoc command and Ansible playbooks click on the highlighted text (hyperlinks) to read our startup articles.
With no further ado, let's go to the objective.
How to copy files with Ansible - Local to Remote
The following playbook copies a file named index.html
from the Ansible control machine ( localhost mac/windows/Linux where we run the playbook) to the remote server
Remember Ansible is agentless so you do not have to install Ansible on the remote machine.
All you need is an SSH connection to the remote server. ( with password or SSH key)
- name: Ansible Copy Example Local to Remote hosts: remoteserver tasks: - name: copying file with playbook become: true copy: src: ~/Downloads/index.html dest: /var/www/html owner: apache group: apache mode: 0644
Let me explain this in detail
- hosts: A target host group should be already defined in the ansible inventory aka hosts file
- tasks: all the tasks (plays) would be defined under this
- become: this is to tell ansible to execute the corresponding task as a sudo user
root
unless specified any other user withbecome_user
- copy: module name we are going to use in this task
- src: source file path on the local machine where the playbook or ad-hoc command is invoked ( can set ansible to look for the file in remote server using
remote_src
as well ) - dest: destination path on the remote server/host where the file should be copied to
- owner: Owner of the file at the destination server once copied
- group: Group of the file at the destination server once copied
- mode: setting the permission of the file after copying.
0644
would be set as permission on the filerw- r – r--
- src: source file path on the local machine where the playbook or ad-hoc command is invoked ( can set ansible to look for the file in remote server using
this can be executed in a single line as an ad hoc command as well.
ansible remoteserver1 -m copy -a "src=~/Downloads/index.html dest=/var/www/html owner=apache group=apache mode=0644"
Here is the execution output of these commands in a short clip
How to copy directories with Ansible - Local to Remote
You can copy directories with Ansible copy module. But there are two variations while copying the directory
- Type1: Copy the Source Directory's contents (but not the directory)
- Type2: Copy the Source Directory and its contents
while the preceding key points look similar, they are not.
In Type#1 only the contents of the src directory would be copied to the destination directory.
In Type#2, Ansible copies the directory to the remote server along with the content of the directory. Ansible would take care of creating the new directory at the remote server
Hope the following image with tree
command output helps to understand this point.
Type#1 Copy directory's content with ansible in a recursive manner
In this Type, Only the Directory's content would be copied. the content can be a directory(subdirectory) or a file.
This is an ansible AD HOC command to copy a directory's content to the remote server
ansible remoteserver -m copy -a "src=~/Downloads/logos/ dest=/var/www/html/ owner=apache group=apache mode=0644 " -i ansible_hosts -b
You need to notice that there is a /
at the end of src
path~/Downloads/logos/
If there is no slash at the end that is Type#2
Refer the following screenshot of executing this ansible ad hoc command and the results on the remote dest directory.
This is a playbook form of the same ad hoc command
- name: Ansible Copy Directory Example Local to Remote hosts: remoteserver tasks: - name: Copying the Directory's contents (sub directories/files) become: true copy: src: ~/Downloads/logos/ dest: /var/www/html owner: apache group: apache mode: 0644
Type#2 Copy directory and it's content with ansible in a recursive manner
In this Type, a directory and its content both would be copied to the remote server.
A new directory might be created on the destination server in the same name matching the src.
This is an ansible AD HOC command to copy a directory to the remote server
ansible remoteserver -m copy -a "src=~/Downloads/logos dest=/var/www/html/ owner=apache group=apache mode=0644 " -i ansible_hosts -b
You need to notice that there is no /
at the end of src
path. It is just ~/Downloads/logos
If you put a slash at the end that is Type#1
Refer the following screenshot of executing this ansible ad hoc command and the results on the remote dest directory.
- name: Ansible Copy Directory Example Local to Remote hosts: remoteserver tasks: - name: Copying the Directory and its contents become: true copy: src: ~/Downloads/logos dest: /var/www/html owner: apache group: apache mode: 0644
We have a continuation of this post covering the ansible fetch module and how to copy remote to the local machine. ( right now in progress )
Stay subscribed and be informed.
For now. Cheers
Sarav
Follow me on Linkedin My Profile Follow DevopsJunction onFacebook orTwitter For more practical videos and tutorials. Subscribe to our channel
Signup for Exclusive "Subscriber-only" Content