2017-03-17 47 views
1

我試圖找到一個例子,我可以從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} 

回答

1

您應該仔細閱讀關於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' 

記住synchronizersync的包裝,因此,對於這種設置工作,必須有SSH-目標主機之間的連接(通常你在控制主機和目標主機之間有ssh連接)。我爲此使用代理轉發。

+0

謝謝。在我發佈之前,我確實閱讀了大量文檔。我發現缺乏實例的可靠文檔。有些人從閱讀課本中學習,有些人喜歡用書中的例子學習。 –

相關問題