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). The get_url module can be used to copy a file from a remote URL to the managed node.

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.

- name: "copy /etc/ansible/roles/your_role/files/foo.txt"
  copy:
    src: "foo.txt"
    dest: "/tmp/foo.txt"

 


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"

 


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.

- name: copy 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.

- name: "copy bar directory"
  copy:
    src: "/tmp/bar/"
    dest: "/tmp/bar/"

 


Optional Parameters

The copy module accepts the following parameters.

  • group (e.g. group: "wheel")
  • mode (e.g. mode: "2775")
  • owner (e.g. owner: "root")
  • remote_src - Do something on the managed node (e.g. remote_src: "yes")
  • seuser - SELinux user (e.g. seuser: "unconfined_u")
  • serole - SELinux role (e.g. serole: "object_r")
  • setype - SELinux type (e.g. setype: "httpd_sys_content_t")
  • selevel - SELinux level (e.g. selevel: "s0")
  • validate - Validate file

 

Here is an example of how these parameters would be used.

- name: "copy foo.txt to /tmp"
  copy:
    src: "foo.txt"
    dest: "/tmp"
    group: "foo"
    mode: "0644"
    owner: "foo"
    remote_src: "yes"
    seuser: "unconfined_u"
    serole: "object_r"
    setype: "httpd_sys_content_r"
    selevel: "s0"
    validate: "/tmp/foo.txt %s"

 


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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |