As I understand every ansible task is executed in context of some host. Let's call it "remote host".
For some ansible modules (f.e. fetch
and copy
) two hosts are involved: the "remote host" and the host where the ansible script is running (let's call it "local host").
Let's say we want to copy a file between two different remote hosts. How to copy files between two nodes using ansible suggests the following solution:
- hosts: ServerB
tasks:
- name: Transfer file from ServerA to ServerB
synchronize:
src: /path/on/server_a
dest: /path/on/server_b
delegate_to: ServerA
Looks like the effect of delegate_to
in this case is that the task still runs in context of ServerB (remote host), but instead of local host the host specified by delegate_to
is used (ServerA in this case).
But the behavior is quite different in the following case:
- hosts: ServerB
tasks:
- name: Copy file from local host to Server A
copy:
src: /local/path
dest: /path/on/server_a
delegate_to: ServerA
Even though values of hosts
and delegate_to
are the same in previous case, the copy operation happens between the local host and the host specified by delegate_to
. So in this case delegate_to
modifies not the local host, but remote host.
Why the behavior of delegate_to
is different in these cases? Is it module-specific? But this parameter is not mentioned in ansible documentation of synchronize
and copy
modules, so I expected it's behavior should be the same for all modules.