如何使用ansible在两个节点之间复制文件

use*_*188 86 ansible

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

我尝试过以下方法:

在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
Run Code Online (Sandbox Code Playgroud)

这种方法一直持续下去.

使用获取和复制模块

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
Run Code Online (Sandbox Code Playgroud)

这种方法给我一个错误如下:

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>'
Run Code Online (Sandbox Code Playgroud)

任何的意见都将会有帮助.

ant*_*t31 93

要复制远程到远程文件,可以将同步模块与' delegate_to: source-server'关键字一起使用:

- hosts: serverB
  tasks:    
   - name: Copy Remote-To-Remote (from serverA to serverB)
     synchronize: src=/copy/from_serverA dest=/copy/to_serverB
     delegate_to: serverA
Run Code Online (Sandbox Code Playgroud)

这个剧本可以从你的机器C运行.

  • 这实际上将文件从serverB复制到serverA.如果要将它们从serverA复制到serverB,请使用`mode = push`(或`delegate_to:serverB`,但不能同时使用两者). (9认同)
  • 从 Vagrant 1.7.x 开始,它根据机器使用不同的私钥。请参阅问题 https://github.com/mitchellh/vagrant/issues/4967 将以下行插入 Vagrantfile `config.ssh.insert_key = false` 以强制 Vagrant 使用 ONE insecure_key 访问所有机器。但是现在我什至没有收到错误消息(它永远等待)。还有 bug https://github.com/ansible/ansible/issues/7250 说不可能从远程复制到远程。 (2认同)
  • @MariusGedminas你是对的,应该使用`mode = push`,但在这种情况下`delegate_to:serverB`不能使用,因为这会使`serverB`成为源和目的地. (2认同)

Flo*_*ker 85

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

您可以将任务放在任一ServerA或上ServerB,但您必须相应地调整传输方向(使用mode参数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
Run Code Online (Sandbox Code Playgroud)

这使用默认值mode: push,因此文件从delegate(ServerA)传输到当前的remote(ServerB).

这可能听起来很奇怪,因为任务已被放置ServerB(通过hosts: ServerB).但是,必须记住,任务实际上是在委托主机上执行的,在这种情况下是ServerA.所以推(从)ServerAServerB确实是正确的方向.还要记住,我们不能简单地选择不委托,因为这意味着转移发生在控制机器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
Run Code Online (Sandbox Code Playgroud)

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

  • 这是一个很好的答案它应该是[Ansible文档](http://docs.ansible.com/ansible/synchronize_module.html)的一部分.这些例子中没有一个以如此清晰的方式解释这一点.谢谢! (6认同)
  • 我已经以多种方式尝试过这个,但是在“警告:身份文件/Users/myuser/.ssh/id_servers 不可访问”上失败了。 (2认同)
  • @WilliamTurrell我已经更新了我的答案,更详细地解释了转移方向.该模块确实有点令人困惑. (2认同)

use*_*188 3

我能够使用 local_action to scp to file 从 machineA 到 machineC 然后将文件复制到 machineB 来解决此问题。