Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronize from localhost to another host which is not the playbook host

Tags:

ansible

rsync

I'm running an Ansible playbook for host_a. Some tasks I delegate to host_b. Now I would like to use the synchronize module to copy a directory from localhost to host_b. But delegate_to is the wrong option here, since this results in copying from host_b to host_a.

Is there a possibility to do that?

- hosts: host_a

  tasks:

  - name: rsync directory from localhost to host_b
    synchronize:
      # files on localhost
      src: files/directory

      dest: /directory/on/host_b

    # delegate_to does not work here
    # delegate_to: host_b

The only solution I can think of is deleting the target directory and then using a recursive copy with the copy module.

I couldn't find anything in the module documentation.

(Using ansible 2.4.2.0)

Doing this task in its own play for host_b is also not really an option because the variables I need for this task depend on host_a.

like image 967
tinita Avatar asked Sep 18 '25 16:09

tinita


1 Answers

The easiest solution in this case is to use rsync command with local_action, i.e

- hosts: cache1
  tasks:
  - name: rsync directory from localhost to host_b
    local_action: command rsync -az "{{ playbook_dir }}/files/directory" "{{ hostvars['host_b']['ansible_host'] }}:/directory/on/host_b"

{{ playbook_dir }} helps by not hardcoding paths on local system.

like image 55
Andrew Avatar answered Sep 23 '25 06:09

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!