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

2024-04-19

作为用户,我想将文件从节点1复制到节点2。是否可以使用复制模块+ delegate_to

以下是我试图做的事情。 Playbook 从节点 3 运行。

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

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

方法1:从master推送,需要在master中启用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:使用fetch和copy模块

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

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用嵌套 delegate_to 将文件从一台远程服务器复制到另一台远程服务器 的相关文章

随机推荐