howtouselinux

7 Ansible Copy Module Examples to Copy File to Remote Server

Table of Contents

Ansible copy module is used to copy the file from the ansible machine to the remote server.

With the ansible copy module, we can do various things let us see what we can do with the ansible copy module.

In this article, we will see 5 ansible copy modules examples and how to copy the files from ansible master to remote server.

Copy file to a remote machine with relative path

In this example, devops.txt file in the ansible machine will be copied to the destination location in the remote server.

But if the same file(with the same name) already exists in the destination location in the remote server, it will replace with the file from ansible machine.

It will not take care of the file is existed or not exist in a remote location. Blindly it will copy the file from ansible machine to the remote server.

– name: copy file from local host to remote host (relative path, ./files/)
copy:
src: devops.txt
dest: $HOME/devops.txt

Copy file to a remote machine with absolute path

In this example test_file file in the ansible machine will be copied to the destination location in the remote server.

– name: copy file from local host to remote host (absolute path)
copy:
src: /path/to/ansible/files/test_file
dest: $HOME/test_file

Copy multiple files to a remote server

we can use with_items to copy more files.

Ansible with_items plugin is a widely used plugin. We will use it whenever we need loop arrangement in our playbook, because this is the standard lookup used mostly.

When we pass a list of items to a task, then task will be performed for all items in that list.

– name: Copy the binary files into /etc/init.d
copy:
src: “{{ item }}”
dest: /etc/init.d
owner: root
group: root
mode: 0755
with_items:
– consul
– keymanager
– vault
– tarball.tar.gz

Copy multiple files to multiple destinations

– name: Copy some files to /etc/myapp/
copy:
src: {{ item.src }}
dest: {{ item.dest }}
owner: root
group: root
mode: u=rw, g=rw, o=r
with_items:
– { src: app1.conf, dest: /etc/app1/ }
– { src: app2.conf, dest: /etc/app2/ }
– { src: app3.conf, dest: /etc/app3/ }

Copy files between locations on the remote host

Ansible copy allows us to copy the files from one directory to another on the same remote machine. But this is only for files, not for the directories. we can use the remote_src parameter to let Ansible know our intentions.

 – name: copy file between locations on the remote host
copy:
src: $HOME/test_file
remote_src: true
dest: $HOME/test_file2

Writing a simple templated text file

The test_file will be created with content “Hello, {{ ansible_user }}!” on the remote server.

– name: write templated text content into a file on remote
copy:
dest: $HOME/test_file
content: “Hello, {{ ansible_user }}!”

Copy Files between local and Remote Machine with one ansible command

We can also use command line to copy files between local and Remote Machine.

The below command will copy test.txt to /tmp/test.txt.

  • ansible test_node -i inventory -m copy -a “src=test.txt dest=/tmp/test.txt”

In this example, we can see that the -a parameter has a src and dest key. The src value refers to the file on the Ansible control node, while dest refers to the path on the remote node

we can use the following command to verify the content of the file on the remote host.

  • ansible test_node -i inventory -m shell -a “cat /tmp/text.txt”

Welcome to howtouselinux.com!

Our website is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative, and we welcome your feedback and suggestions for future content.

Learn More

Facebook
Twitter
LinkedIn