save file in local machine which was generate in remote machine?

筅森魡賤 提交于 2019-12-11 13:36:50

问题


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

I run test.yml in local machine, it will run the shell script in remote machine and save the command in system.txt file.

location of top.sh: remote, location of system.txt: remote

But I am looking for

location of top.sh: local (but I need to run this command in remote), location of system.txt: local

How to achieve this?


回答1:


You can use the synchronize module to copy files from local to remote or from remote to local host. For example

- 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: .

Note: for the synchronize module to work you will need to have rsync installed on the hosts.



来源:https://stackoverflow.com/questions/37943550/save-file-in-local-machine-which-was-generate-in-remote-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!