2017-09-21 3 views
0

現在、2つのホストを持ってグループに動的に追加し、synchronizeタスクの後にwith_togetherタスクを使用して2つの要素の3つのリストを使用して、リモートサーバー。with_togetherファッションのホストに対して実行可能なタスク

--- 
- name: Configure Hosts for Copying 
    hosts: localhost 
    gather_facts: no 
    tasks: 

    - name: Adding given hosts to new group... 
     add_host: 
     name: "{{ item }}" 
     groups: copy_group 
     with_items: 
     - ["remoteDest1", "remoteDest2"] 


- name: Copy Files between servers 
    hosts: copy_group 
    gather_facts: no 
    tasks:  

    - name: Copying files... 
     synchronize: 
     src: "{{ item[1] }}" 
     dest: "{{ item[2] }}" 
     with_together: 
     - ["remoteSrc1", "remoteSrc2"] 
     - ["/tmp/remote/source/one/", "/tmp/remote/source/two/"] 
     - ["/tmp/remote/dest/one/", "/tmp/remote/dest/two/"] 
     delegate_to: "{{ item[0] }}" 

現在のところ、それは4回の操作で、その結果、両方のサーバーの両方の操作を行います。

は、ここでの考え方に基づい例です。それは1です意味しますremoteDest2

/tmp/remote/dest/two/からremoteSrc2から/tmp/remote/dest/one/

  • コピー/tmp/remote/source/one/remoteSrc1からコピー/tmp/remote/source/two/
  • remoteDest1上:

    私はそうのように同期させる必要がある

    : 1比;基本的には、ホストに対してはwith_togetherと同じ方法で動作します。

    ホストは動的に取得されるため、ホストごとに異なるプレイを行うことはできません。

    synchronizeは基本的にrsyncの簡略化されたバージョンであるため、rsyncを使用してこの簡単な解決策がある場合は、非常に感謝します。

答えて

0

あり、このためのネイティブ機能がないので、これは私がそれを解決する方法である:元のタスクを考えると

、次の2行を追加します取得するには

- "{{ groups['copy_group'] }}" 
when: inventory_hostname == item[3] 

を:

- name: Copying files... 
    synchronize: 
    src: "{{ item[1] }}" 
    dest: "{{ item[2] }}" 
    with_together: 
    - ["remoteSrc1", "remoteSrc2"] 
    - ["/tmp/remote/source/one/", "/tmp/remote/source/two/"] 
    - ["/tmp/remote/dest/one/", "/tmp/remote/dest/two/"] 
    - "{{ groups['copy_group'] }}" 
    delegate_to: "{{ item[0] }}" 
    when: inventory_hostname == item[3] 

本質的に、ホストをリストとして追加することにより、ホストをwhenステートメントで使用して、現在のホスト(​​)マット現在リストにインデックスされているホストをチェスします。

結果は、同じインデックスを持つ他のリスト項目との間で、各ホストに対して1回だけ連続して再生されます。

関連する問題