您應該仔細閱讀關於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連接)。我爲此使用代理轉發。
謝謝。在我發佈之前,我確實閱讀了大量文檔。我發現缺乏實例的可靠文檔。有些人從閱讀課本中學習,有些人喜歡用書中的例子學習。 –