使用嵌套的委托委托将文件从一台远程服务器复制到另一台远程服务器

时间:2019-07-10 19:02:19

标签: ansible

作为用户,我想将文件从节点1复制到节点2。复制模块+ proxy_to

是否可以

以下是我正在尝试做的事情。 Playbook正在从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

2 个答案:

答案 0 :(得分:2)

简短的回答是:您将无法使用keep-alive 44782 127.0.0.1 - - [10/Jul/2019 16:09:09] "POST / HTTP/1.1" 200 - keep-alive 44784 127.0.0.1 - - [10/Jul/2019 16:09:09] "POST / HTTP/1.1" 200 - keep-alive 44786 127.0.0.1 - - [10/Jul/2019 16:09:09] "POST / HTTP/1.1" 200 - keep-alive 44788 127.0.0.1 - - [10/Jul/2019 16:09:09] "POST / HTTP/1.1" 200 - keep-alive 44790 127.0.0.1 - - [10/Jul/2019 16:09:09] "POST / HTTP/1.1" 200 - keep-alive 44792 127.0.0.1 - - [10/Jul/2019 16:09:09] "POST / HTTP/1.1" 200 - keep-alive 44794 127.0.0.1 - - [10/Jul/2019 16:09:09] "POST / HTTP/1.1" 200 - keep-alive 44796 127.0.0.1 - - [10/Jul/2019 16:09:09] "POST / HTTP/1.1" 200 - keep-alive 44798 127.0.0.1 - - [10/Jul/2019 16:09:09] "POST / HTTP/1.1" 200 - keep-alive 44800 127.0.0.1 - - [10/Jul/2019 16:09:09] "POST / HTTP/1.1" 200 - 模块来完成此操作。

不过,您可能想看看synchronize module

引用文档

  

可以使用委托_to将“本地主机”更改为其他主机。这样一来,便可以在两个远程主机之间复制,也可以在一个远程计算机上完全复制。

基本上,您最终会得到类似的东西:

copy

编辑,我刚刚发现this article引用了--- - 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 以及您可能要查看的fetch / copy方法。

答案 1 :(得分:2)

仅当在源服务器(在您的情况下为kube master)或kube节点中启用rsync时,才可以使用syncize模块。

方法1:要从主服务器推送,需要在主服务器上启用rsync

默认情况下同步使用push模式

- hosts: nodes
  tasks:
    - name: Transfer file from master to nodes
      synchronize:
        src: /src/path/to/file
        dest: /dest/path/to/file
      delegate_to: "{{ master }}"

方法2:使用获取和复制模块

 - 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' }}"

希望这会有所帮助。