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.

The default behavior of the copy module is to copy a file or directory from the control node (that's your Ansible server) to the managed nodes (e.g. target systems). The remote_src parameter can be used to copy a file or directory on the managed node to some other directory on the managed node. Or Role Files or Role Templates could be used.

The get_url module can be used to copy a file from a remote URL to the managed node. Or,the synchronize module can be used for both local and remote copies via rsync.


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
    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
    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
    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
    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
    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
    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
    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
    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
    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 fe4aed in the box below so that we can be sure you are a human.