如何使用 Ansible 将远程文件复制到本地计算机上?

Mus*_*med 3 ansible

我在我的剧本中使用命令模块,它目前看起来像这样。

- hosts: all

  tasks:   
   - name: Update tar file
     command: sudo scp -r username@hostname:/path/from/destination /path/to/destination
Run Code Online (Sandbox Code Playgroud)

出于可读性的目的,我省略了在此任务之前发生的任务,但是当我运行此 playbook 时,它会在此任务处停止。它根本不会前进。我确定这是因为 sudo,所以它可能需要密码。但是,我不确定如何解决这个问题。

Gar*_*yde 7

您想使用fetch 模块

- hosts: host.example.com
  tasks:
    # Copy remote file (host.example.com:/tmp/somefile) into
    # /tmp/fetched/host.example.com/tmp/somefile on local machine
    - fetch:
        src: /tmp/somefile
        dest: /tmp/fetched
Run Code Online (Sandbox Code Playgroud)

  • 更新的文档链接:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/fetch_module.html。 (3认同)
  • @BünyaminŞentürk 是的。请参阅 [`flat` 参数](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/fetch_module.html#parameter-flat)。 (2认同)