FreeKB - copy module
Ansible - copy module

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 node (e.g. target system).


Copy file

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

- name: copy foo.txt
  copy:
    src: /tmp/foo.txt
    dest: /tmp/foo.txt
    owner: foo
    group: foo
    mode: '0644'

 

Copy directory

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

- name: copy bar directory
  copy:
    src: /tmp/bar
    dest: /tmp/bar
    owner: bar
    group: bar
    mode: '0775'

 


From managed node to managed node

remote_src: yes is used to copy a file from one directory on a managed node to another directory on the managed node. In this example, /root/foo.txt is copied to /tmp/foo.txt on the managed node.

- name: copy foo.txt to /tmp
  copy:
    src: /root/foo.txt
    dest: /tmp/foo.txt
    remote_src: yes
    owner: foo
    group: foo
    mode: '0644'

 

Likewise, a directory on the managed node can be copied to a new location on the managed node. In this example, the /tmp/foo directory is copied to /tmp/bar on the managed node.

- name: copy /tmp/foo directory to /tmp/bar
  copy:
    src: /tmp/foo
    dest: /tmp/bar
    remote_src: yes
    owner: foo
    group: foo
    mode: '0775'

 


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.

- 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.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments