2017-08-24 26 views
0

我想使用Ansible的synchronize模塊根據條件將文件傳輸到遠程服務器。條件是目錄及其內容存在於源服務器上。如果目錄存在,Ansible有條件同步

例如:

先玩,檢查是否在本地主機所在的目錄:

hosts: localhost 
stat: 
    path: some/path/to/dir/ 
register: st 

然後,在第二個上場,使用該變量在when聲明有條件使用synchronize模塊:

hosts: remote_hosts 
synchronize: 
    src: some/path/to/dir/ 
    dest: some/dest/path 
when: st.stat.isdir is defined 

我的問題是,st變量不能播放之間使用,所以我怎麼能Ø如果目錄存在,那麼只需運行synchronize任務。

我會把它們放在同一個遊戲中,但他們使用不同的主機,你可以看到。

我的安心版本是2.3.1.0

回答

1

你有幾個選項。您可以將remote_users放置在本地主機任務中,並使用delegate_to: localhost

stat: 
    path: some/path/to/dir/ 
register: st 
delegate_to: localhost 

或者你可以保持本地主機劇中是和在remote_hosts使用hostvars參考本地主機的變量發揮。

hosts: remote_hosts 
synchronize: 
    src: some/path/to/dir/ 
    dest: some/dest/path 
when: hostvars['localhost']['st'].stat.isdir is defined 
+0

感謝您提供多種方法;我沒有看到任何地方使用這些方法。我正要嘗試像https://stackoverflow.com/questions/33348077/how-to-conditionally-execute-group-of-tasks-on-ansible之類的東西,但你的更簡潔 –