您应该仔细阅读关于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
库存:
[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'
记住synchronize
是rsync
的包装,因此,对于这种设置工作,必须有SSH-目标主机之间的连接(通常你在控制主机和目标主机之间有ssh连接)。我为此使用代理转发。
谢谢。在我发布之前,我确实阅读了大量文档。我发现缺乏实例的可靠文档。有些人从阅读课本中学习,有些人喜欢用书中的例子学习。 –