如何使用ansible在两个节点之间复制文件 [英] How to copy files between two nodes using ansible

查看:129
本文介绍了如何使用ansible在两个节点之间复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文件从机器 A 复制到机器 B,而我运行所有 ansible 任务的控制机器是机器 C(本地机器)

I need to copy file form machine A to machine B whereas my control machine from where i run all my ansible tasks is machine C(local machine)

我尝试了以下方法:

在ansible的shell模块中使用scp命令

hosts: machine2
user: user2
tasks:
  - name: Copy file from machine1 to machine2 
    shell: scp user1@machine1:/path-of-file/file1 /home/user2/file1

这种方法一直持续下去,永无止境.

This approach just goes on and on never ends.

使用 fetch &复制模块

hosts: machine1
user: user1
tasks:
  - name: copy file from machine1 to local
    fetch: src=/path-of-file/file1 dest=/path-of-file/file1

hosts: machine2
user: user2
tasks:
  - name: copy file from local to machine2
    copy: src=/path-of-file/file1 dest=/path-of-file/file1

这种方法给我带来了如下错误:

This approach throws me an error as follows:

error while accessing the file /Users/<myusername>/.ansible/cp/ansible-ssh-machine2-22-<myusername>, error was: [Errno 102] Operation not supported on socket: u'/Users/<myusername>/.ansible/cp/ansible-ssh-machine2-22-<myusername>'

任何建议都会有所帮助.

Any suggestions would be helpful.

推荐答案

正如 ant31 已经指出的,您可以使用 synchronize 模块到此.默认情况下,模块在控制机器和当前远程主机 (inventory_host) 之间传输文件,但是可以使用任务的 delegate_to 参数(重要的是要注意这是任务的参数,而不是模块).

As ant31 already pointed out you can use the synchronize module to this. By default, the module transfers files between the control machine and the current remote host (inventory_host), however that can be changed using the task's delegate_to parameter (it's important to note that this is a parameter of the task, not of the module).

您可以将任务放置在 ServerAServerB 上,但您必须相应地调整传输方向(使用 modesynchronize 的参数).

You can place the task on either ServerA or ServerB, but you have to adjust the direction of the transfer accordingly (using the mode parameter of synchronize).

将任务放在ServerB

- hosts: ServerB
  tasks:
    - name: Transfer file from ServerA to ServerB
      synchronize:
        src: /path/on/server_a
        dest: /path/on/server_b
      delegate_to: ServerA

这使用默认的mode: push,因此文件从委托(ServerA)传输到当前远程(ServerB).

This uses the default mode: push, so the file gets transferred from the delegate (ServerA) to the current remote (ServerB).

这听起来可能很奇怪,因为任务已经放在ServerB(通过hosts:ServerB).但是,必须记住,任务实际上是在委托主机上执行的,在本例中为 ServerA.所以推(从ServerAServerB)确实是正确的方向.还要记住,我们不能简单地选择不委托,因为这意味着传输发生在控制机器ServerB之间.

This might sound like strange, since the task has been placed on ServerB (via hosts: ServerB). However, one has to keep in mind that the task is actually executed on the delegated host, which in this case is ServerA. So pushing (from ServerA to ServerB) is indeed the correct direction. Also remember that we cannot simply choose not to delegate at all, since that would mean that the transfer happens between the control machine and ServerB.

将任务放在ServerA

- hosts: ServerA
  tasks:
    - name: Transfer file from ServerA to ServerB
      synchronize:
        src: /path/on/server_a
        dest: /path/on/server_b
        mode: pull
      delegate_to: ServerB

这使用 mode: pull 来反转传输方向.再次记住,任务实际上是在 ServerB 上执行的,因此拉取是正确的选择.

This uses mode: pull to invert the transfer direction. Again, keep in mind that the task is actually executed on ServerB, so pulling is the right choice.

这篇关于如何使用ansible在两个节点之间复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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