如何使用同步模块将文件复制到多个服务器

Ant*_*dev 4 synchronize ansible

我试图找到一个示例,我可以将文件从serverA拉到一组服务器.
mygroup由10台服务器组成.需要将该文件复制到这10个服务器.这是我所拥有的,但它不完全正常.没有处理程序部分,我可以做一对一的复制没问题.

- hosts: serverA
  tasks:
- name: Transfer file from serverA to mygroup
  synchronize:
    src: /tmp/file.txt
    dest: /tmp/file.txt
    mode: pull

  handlers:
- name: to many servers
  delegate_to: $item
  with_items: ${groups.mygroup}
Run Code Online (Sandbox Code Playgroud)

Kon*_*rov 6

您应该仔细阅读有关ansible如何工作的文档(什么是主机模式,什么是策略,什么是处理程序......).

以下是您的问题的答案:

---
# Push mode (connect to xenial1 and rsync-push to other hosts)
- hosts: xenial-group:!xenial1
  gather_facts: no
  tasks:
    - synchronize:
        src: /tmp/hello.txt
        dest: /tmp/hello.txt
      delegate_to: xenial1

# Pull mode (connect to other hosts and rsync-pull from xenial1)
- hosts: xenial1
  gather_facts: no
  tasks:
    - synchronize:
        src: /tmp/hello.txt
        dest: /tmp/hello.txt
        mode: pull
      delegate_to: "{{ item }}"
      with_inventory_hostnames: xenial-group:!xenial1
Run Code Online (Sandbox Code Playgroud)

库存:

[xenial-group]
xenial1 ansible_ssh_host=192.168.168.186 ansible_user=ubuntu ansible_ssh_extra_args='-o ForwardAgent=yes'
xenial2 ansible_ssh_host=192.168.168.187 ansible_user=ubuntu ansible_ssh_extra_args='-o ForwardAgent=yes'
xenial3 ansible_ssh_host=192.168.168.188 ansible_user=ubuntu ansible_ssh_extra_args='-o ForwardAgent=yes'
Run Code Online (Sandbox Code Playgroud)

请记住,这synchronize是一个包装器rsync,因此要使此设置生效,目标主机之间必须存在ssh连接(通常在控制主机和目标主机之间有ssh连接).我使用代理转发.

  • 谢谢你。在我在这里发帖之前,我确实阅读了大量文档。我发现示例中缺少 ansible 文档。有些人从阅读课本中学习,而有些人像我一样通过实例学习。 (2认同)