Sumith08
Sumith08

Reputation: 770

How to copy directories using Ansible on remote machines

I am trying to copy directories on remote machine using Ansible.

It throws "msg": "Remote copy does not support recursive copy of directory:" error.

below is my ansible playbook.

Can anyone please help me?

Upvotes: 1

Views: 3477

Answers (3)

Emros
Emros

Reputation: 115

First: if you want to copy all files and folder to the remote you can use the code below:

- name: "Copy Folder"
  hosts: "all"
  vars:
    source_dir: "/path/to/source/directory"
    dest_dir: "/path/to/destination/directory"
  tasks:
    - name: "Copy files and directories"
      copy:
        src: "{{ source_dir }}/*"
        dest: "{{ dest_dir }}/"

Second one :

- name: "Use Rsync to Copy"
  hosts: "all"
  vars:
    source_dir: "/path/to/source/directory"
    dest_dir: "/path/to/destination/directory"
  tasks:
    - name: "Copy directories"
      synchronize:
        src: "{{ source_dir }}/{{ item }}"
        dest: "{{ dest_dir }}/"
        recursive: "yes"
        delete: "no"
      with_items:
        - dir1
        - dir2
        - dir3

If you use sonar or Ansible linter rename module with ansible.builtin. or ansible.legacy.

And the vars can be in a defaults folder or in vars folder.

Upvotes: 0

Amarnath S
Amarnath S

Reputation: 1

Mostly the code snippet given by you must work ..

But still you want try a workaround . You can use the shell module .

Ex:

- name: Copy the directories to remote server . shell: | scp -r path/of/file user_name@dns_name:/remote/path delegate_to: "{{ inventory_hostname }}"

Upvotes: 0

Dan Vulpe
Dan Vulpe

Reputation: 1326

You can use the synchronize module https://docs.ansible.com/ansible/latest/modules/synchronize_module.html#examples and just replace copy with synchronize.

Read the example link above. For two directories on one remote host use:

- name: Synchronize two directories on one remote host.
  synchronize:
    src: /first/absolute/path
    dest: /second/absolute/path
  delegate_to: "{{ inventory_hostname }}"

Upvotes: 1

Related Questions