Skip to main content

Ansible Copy Cheat Sheet

Shenzhen, China

Copying files from a local machine to the remote server

---
- hosts: test
tasks:
- name: Ansible copy file to remote server
copy:
src: ~/sample.txt
dest: /tmp
---
- hosts: test
tasks:
- name: Ansible copy file force
copy:
src: ~/sample.txt
dest: /tmp
force: no

Copying a directory from a local machine to the remote server

---
- hosts: test
tasks:
- name: Ansible copy directory to the remote server
copy:
src:/Users/mdtutorials2/Documents/Ansible/copy_dir_ex
dest:/Users/mdtutorials2/Documents/Ansible/tmp
output
------
tree tmp

tmp
└── copy_dir_ex
├── file1
├── file2
├── file3
└── tmp2
├── file4
└── file5
---
- hosts: test
tasks:
- name: Ansible copy files from a directory to remote server
copy:
src:/Users/mdtutorials2/Documents/Ansible/copy_dir_ex/
dest:/Users/mdtutorials2/Documents/Ansible/tmp
output
------
tree tmp

tmp/
├── file1
├── file2
├── file3
└── tmp2
├── file4
└── file5

Copying multiple files/directories using with_items

---
- hosts: test
tasks:
- name: Ansible copy multiple files with_items
copy:
src: ~/{{item}}
dest: /tmp
mode: 0774
with_items:
['hello1','hello2','hello3','sub_folder/hello4']

Copying multiple files with different permission/destination settings

---
- hosts: test
tasks:
- name: Copy multiple files in Ansible with different permissions
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{item.mode}}"
with_items:
- { src: '~/test1',dest: '/tmp/system1', mode: '0777'}
- { src: '~/test2',dest: '/tmp/system2', mode: '0707'}
- { src: '~/test3',dest: '/tmp2/system3', mode: '0575'}
ls -lrt /tmp
drwxrwxrwx 2 root root 4096 Oct 9 14:28 system1
drwx---rwx 2 root root 4096 Oct 9 14:28 system2

ls -lrt /tmp2
-r-xrwxr-x 1 root root 0 Oct 9 14:33 system3

Copying all the files inside a folder that matches a pattern

- hosts: test
tasks:
- name: Ansible copy multiple files with wildcard matching.
copy:
src: "{{ item }}"
dest: /etc
with_fileglob:
- /tmp/hello*

Creating backup of a file in the remote servers before copy

For example, the following example will create a backup of the helloworld.txt in the /tmp directory of the remote server.

- hosts: test
tasks:
- name: ansible copy file backup example
copy:
src: ~/helloworld.txt
dest: /tmp
backup: yes

Copying files from remote machine to the local machine

By default, a directory named after each host you are connecting will be created in your destination directory (local machine). The fetched files will be copied there. If the file doesn’t exist on the remote server, no error will be thrown by default. In the following example, I am running the task on remote-server-1. The file will be copied into /etc/remote-server-1/tmp directory of the local machine.

- hosts: test
tasks:
- name: Ansible fetch files from remote server to the local machine using Ansible fetch module
fetch:
src: /tmp/hello2
dest: /etc
mode: 0774

If you don’t want this behavior and you need the file to be copied directly to the destination directory, then you should use the ‘flat’ parameter.

- hosts: test
tasks:
- name: Ansible fetch directory example with flat parameter set
fetch:
src: /tmp/hello2
dest: /tmp/
mode: 0774
flat: yes

The following example will throw an error if the remote file does not exist.

- hosts: test
tasks:
- name: Ansible fetch example with fail_on_missing set
fetch:
src: /tmp/fetch.txt
dest: /tmp/
mode: 0774
fail_on_missing: yes

Writing to a file using the copy module

- hosts: test
tasks:
- name: Ansible write to a file example
- copy:
content: |
Example Content.
echo.txt will be created after this task is executed.
dest: /tmp/echo.txt
backup: yes