2016-06-21 63 views
1

test.yml在遠程機器上生成的本地機器上保存文件?

--- 
- hosts: webservers 
    remote_user: username 
    tasks: 
    - name: Execute the script 
     command: sh /home/username/top.sh 

top.sh

#!/bin/bash 
top > system.txt 

我跑test.yml在本地機器上,它將運行在遠程機器的shell腳本和保存命令在system.txt文件中。

位置 top.sh

system.txt的遙控器,定位:遠程

但是我正在尋找位置top.sh

:本地(但我需要在遠程運行此命令),地址system.txt:本地

如何實現這一目標?

回答

1

您可以使用synchronize模塊將文件從本地複製到遠程或從遠程複製到本地主機。例如

- name: 'Copy run.sh to remote machine' 
    synchronize: 
    mode: push 
    src: top.sh 
    dest: /home/username 

- name: Execute the script 
    command: sh /home/username/top.sh 

- name: 'Copy system.txt to local machine' 
    synchronize: 
    mode: pull 
    src: /home/username/system.txt 
    dest: . 

注:爲synchronize模塊來工作,你將需要安裝在主機rsync

+0

哇,你太棒了。在'執行腳本'命令不會結束它會動態生成內容(無限循環),所以它不會進入下一步(存儲在本地計算機中)。如何克服這種情況? –

+0

@BilalUsean您需要在top.sh腳本中添加某種超時,或者在async和wait_for中使用ansible,但這是一個單獨的問題,您應該問一下。 – SztupY

相關問題