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

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

问题描述

我需要将文件从机器A复制到机器B,而我执行所有任务的控制机器是机器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

这种方法永无止境.

使用访存&复制模块

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.

推荐答案

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

To copy remote-to-remote files you can use the synchronize module with 'delegate_to: source-server' keyword:

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

此剧本可以在您的计算机上运行.

This playbook can run from your machineC.

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

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