在远程机器上生成的本地机器上保存文件?

在远程机器上生成的本地机器上保存文件?

问题描述:

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.