Bootstrap FreeKB - Ansible - Copy a file or directory
Ansible - Copy a file or directory

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

There are a number of similar (but unique) ways to copy files and directories using Ansible.

  • copy can be used to copy files/directories from the Ansible system to the target systems or to copy files/directories on the target systems to some other directory on the target system (this article)
  • get_url can be used to copy a file from a URL to the target systems
  • synchronize can be used for both local and remote copies and is based off of rsync
  • Or Role Files or Role Templates can be used

Copy file from control node to managed nodes

In this example, /tmp/foo.txt on the control node is copied to /tmp/foo.txt on the managed nodes.

---
- hosts: all
  tasks:
  - name: copy foo.txt
    ansible.builtin.copy:
      src: /tmp/foo.txt
      dest: /tmp/foo.txt
      backup: true
...

 


Backup existing file

By default, if the target file exists (dest), the file will be overwritten by the source file (src), and the original file will not be backed up. backup: true can be used to create a backup copy of the original file.

---
- hosts: all
  tasks:
  - name: copy foo.txt
    ansible.builtin.copy:
      src: /tmp/foo.txt
      dest: /tmp/foo.txt
      backup: true
...

 


Create file on managed node

The content parameter can be used to create a file with whatever value is defined in the content parameter. In this example, the /tmp/foo.txt file will contain the text "bar".

---
- hosts: all
  tasks:
  - name: copy foo.txt
    ansible.builtin.copy:
      dest: /tmp/foo.txt
      content: bar
...

 


Copy file on managed node

The remote_src parameter can be used to copy a file on the managed node to some other directory on the managed node.

---
- hosts: all
  tasks:
  - name: copy foo.txt
    ansible.builtin.copy:
      src: /tmp/foo.txt
      dest: /home/john.doe/foo.txt
      remote_src: true
...

 

Or, hosts: localhost can be used.

---
- hosts: localhost
  tasks:
  - name: copy foo.txt
    ansible.builtin.copy:
      src: /tmp/foo.txt
      dest: /home/john.doe/foo.txt
      backup: true
...

 


Copy file using the roles "files" directory

When using roles, one of the roles directories is files, like this.

/etc/ansible/roles/your_role/files

 

One of the parameters of the copy module is src. If src only contains the file name, Ansible will look for foo.txt in the roles files directory, like this.

---
- hosts: all
  tasks:
  - name: copy /etc/ansible/roles/your_role/files/foo.txt
    ansible.builtin.copy:
      src: foo.txt
      dest: /tmp/foo.txt
...

 


Copy directory

In this example, the /tmp/bar directory (and the contents of the bar directory) on the control node is copied to /tmp/bar on the managed nodes.

---
- hosts: all
  tasks:
  - name: copy the 'bar' directory
    ansible.builtin.copy:
      src: /tmp/bar
      dest: /tmp/bar
...

 

In this example, the contents of the /tmp/bar directory on the control node is copied to /tmp/bar on the managed nodes.

---
- hosts: all
  tasks:
  - name: copy the 'bar' directory
    ansible.builtin.copy:
      src: /tmp/bar/
      dest: /tmp/bar/
...

 

Or, you could create an archive of the directory on the control node, such as bar.tar.gz, using the archive module, copy the archive file from the control node to the managed node, and then use the unarchive module to extract the archive on the managed node.

 


loop

The loop module can be used to loop through file. In this example, both foo.txt and bar.txt are copied from the control node to each managed node.

---
- hosts: all
  tasks:
  - name: copy items
    ansible.builtin.copy:
      src: "/tmp/{{ items }}"
      dest: "/tmp/{{ items }}"
      owner: foo
      group: foo
      mode: "0644"
    loop:
      - foo.txt
      - bar.txt
...

 


File copied

If the file was successfully copied, the play should indicate changed.

TASK [copy foo.txt]
changed: [server1.example.com]

 

Following are scenario's where the file will be copied:

  • The file does not exist on the managed node
  • The file exists on the managed node but there are differences between the file on the control node and the file on the managed node

 

File not copied

If the file was not copied, the play should indicate ok. This is the expected behavior if the file already exists on the managed node and there are no differences between the file on the control node and the file on the managed node. In this scenario, the file on the managed node will not be created, overwritten, or changed.

TASK [copy foo.txt]
ok: [server1.example.com]

 

Destination not writable

If Destination not writable is returned when running the play, refer to our article on resolving Destination not writable.

 


You may also want to use x.stat.exists to determine if the file exists.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter d6eae0 in the box below so that we can be sure you are a human.