在远程机器上生成的本地机器上保存文件? [英] save file in local machine which was generate in remote machine?

查看:36
本文介绍了在远程机器上生成的本地机器上保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 文件中.

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

top.sh 的位置:远程,system.txt 的位置:远程

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

但我正在寻找

top.sh 的位置:本地(但我需要在远程运行此命令),system.txt 的位置:本地

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

如何实现这一目标?

推荐答案

您可以使用 synchronize 模块将文件从本地复制到远程或从远程到本地主机.例如

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

注意:要使 synchronize 模块工作,您需要在主机上安装 rsync.

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

这篇关于在远程机器上生成的本地机器上保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆