gpt4 book ai didi

ansible - 使用嵌套的 delegate_to 将文件从一个远程服务器复制到另一个远程服务器

转载 作者:行者123 更新时间:2023-12-02 16:54:37 29 4
gpt4 key购买 nike

作为用户,我想将文件从 node1 复制到 node2。 copy module + delegate_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

最佳答案

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

方法一:从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 }}"

方法二:使用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' }}"

希望这对您有所帮助。

关于ansible - 使用嵌套的 delegate_to 将文件从一个远程服务器复制到另一个远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56977103/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com