0
votes

I'm doing ansible remote copy, have a question about dest, is there any difference if the dest ends with or without /?

  1. dest: /tmp/dest
  2. dest: /tmp/dest/

I've tried with and without /, looks like both of them do the copy.

    - name: copy the properties file to dest
      copy:
        src: /tmp/src/{{ item }}
        dest: /tmp/dest
        remote_src: yes
      with_items:
        - runtime.properties
        - default.properties
1

1 Answers

1
votes

If you are copying a directory, it doesn't matter whether or not the target path ends with /. In both cases, Ansible first ensure the target directory exists and then copy the source directory into the target directory. That is, given either:

- copy:
    src: src_dir
    dest: /tmp/dest/

Or:

- copy:
    src: src_dir
    dest: /tmp/dest

In both cases, Ansible will first create /tmp/dest if it does not exist and will then create /tmp/dest/src_dir and populate it with the contents of src_dir.

However, if you are copying a file the situation is a little different. If the target destination /tmp/dest does not exist, this playbook will create a file named /tmp/dest:

- copy:
    src: src_file
    dest: /tmp/dest

However, if you add a trailing path to the destination, then Ansible will first create the directory /tmp/dest and then create the file /tmp/dest/src_file.

- copy:
    src: src_file
    dest: /tmp/dest/

If a directory named /tmp/dest already exists, then both of the above examples will do the same thing (create /tmp/dest/src_file).