问题
As a user I want to copy file from node1 to node2. Is it possible with copy module + delegate_to
Below is what I was trying to do. Playbook is running from node3.
Playbook Sample
---
- name: Gather Facts for all hosts
hosts: all
gather_facts: true
any_errors_fatal: true
become: true
- name: Test
hosts: all
gather_facts: false
any_errors_fatal: true
become: true
roles:
- role: test
Role Sample
---
- block:
- include_tasks: test.yml
any_errors_fatal: true
run_once: true
Task Sample
---
- name: Test
block:
- name: Transfer files from node1 to node2
copy:
src: /tmp/a
dest: /tmp/
delegate_to: node2
delegate_to: node1
回答1:
The short answer is no: you will not be able to do this with the copy module.
You might however want to have a look at the synchronize module
Quoting the doc
The “local host” can be changed to a different host by using delegate_to. This enables copying between two remote hosts or entirely on one remote machine.
You would basically end up with something like:
---
- name: Rsync some files
hosts: my_target_host
tasks:
- name: copy my file
synchronize:
src: path/on/source/host
dest: path/on/dest/host
delegate_to: my_source_host
Edit I just found this article referencing synchronize as well as the fetch/copy method that you might want to look at.
回答2:
You can use synchronize module only when rsync is enabled either in source server (kube master in your case) or in the kube nodes.
Method 1: to push from master, need rsync enabled in master
Synchronize use push mode by default
- hosts: nodes
tasks:
- name: Transfer file from master to nodes
synchronize:
src: /src/path/to/file
dest: /dest/path/to/file
delegate_to: "{{ master }}"
Method 2: to use fetch and copy modules
- hosts: all
tasks:
- name: Fetch the file from the master to ansible
run_once: yes
fetch: src=/src/path/to/file dest=temp/ flat=yes
when: "{{ ansible_hostname == 'master' }}"
- name: Copy the file from the ansible to nodes
copy: src=temp/file dest=/dest/path/to/file
when: "{{ ansible_hostname != 'master' }}"
Hope this helps.
来源:https://stackoverflow.com/questions/56977103/copy-file-from-one-remote-server-to-another-remote-server-using-nested-delegate