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

时间:2014-08-26 11:49:42

标签: 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

这种方法一直持续下去。

使用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

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

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>'

任何建议都会有所帮助。

8 个答案:

答案 0 :(得分:83)

要复制远程到远程文件,您可以将synchronize模块与&#39; delegate_to: source-server&#39;一起使用。关键字:

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

此剧本可以从您的machineC运行。

答案 1 :(得分:76)

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

您可以将任务放在ServerAServerB上,但必须相应地调整转移方向(使用mode的{​​{1}}参数。

将任务放在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上(通过ServerB)。但是,必须记住,任务实际上是在委派主机上执行的,在这种情况下是hosts: ServerB。所以推动(从ServerAServerA)确实是正确的方向。还要记住,我们不能简单地选择不委托,因为这意味着转移发生在控制机器ServerB之间。

将任务放在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上执行的,因此提取是正确的选择。

答案 2 :(得分:2)

我能够使用local_action解决这个问题,将scp文件从machineA发送到machineC,然后将文件复制到machineB。

答案 3 :(得分:2)

如果您需要通过ansible在两个远程节点之间同步文件,可以使用:

- name: synchronize between nodes
  environment:
    RSYNC_PASSWORD: "{{ input_user_password_if_needed }}"
  synchronize:
    src: rsync://user@remote_server:/module/
    dest: /destination/directory/
    // if needed
    rsync_opts:
       - "--include=what_needed"
       - "--exclude=**/**"
    mode: pull
    delegate_to: "{{ inventory_hostname }}"

remote_server上时,您需要使用守护程序模式启动rsync。简单的例子:

pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsync.log
port = port

[module]
path = /path/to/needed/directory/
uid = nobody
gid = nobody
read only = yes
list = yes
auth users = user
secrets file = /path/to/secret/file

答案 4 :(得分:0)

使用复制模块将文件从一台服务器传输到另一台服务器的简单方法

这是剧本

---
- hosts: machine1 {from here file will be transferred to another remote machine}
  tasks:
  - name: transfer data from machine1 to machine2

    copy:
     src=/path/of/machine1

     dest=/path/of/machine2

    delegate_to: machine2 {file/data receiver machine}

答案 5 :(得分:0)

如果要进行rsync并使用自定义用户和自定义ssh密钥,则需要在rsync选项中编写此密钥。

---
 - name: rsync
   hosts: serverA,serverB,serverC,serverD,serverE,serverF
   gather_facts: no
   vars:
     ansible_user: oracle
     ansible_ssh_private_key_file: ./mykey
     src_file: "/path/to/file.txt"
   tasks:
     - name: Copy Remote-To-Remote from serverA to server{B..F}
       synchronize:
           src:  "{{ src_file }}"
           dest: "{{ src_file }}"
           rsync_opts:
              - "-e ssh -i /remote/path/to/mykey"
       delegate_to: serverA

答案 6 :(得分:0)

您也可以将deletgatescp一起使用:

- name: Copy file to another server
  become: true
  shell: "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null admin@{{ inventory_hostname }}:/tmp/file.yml /tmp/file.yml"
  delegate_to: other.example.com

由于delegate,该命令在另一台服务器上运行,并且scp本身就是文件。

答案 7 :(得分:0)

在 2021 年,您应该安装包装器:

ansible-galaxy collection install ansible.posix

并使用

- name: Synchronize two directories on one remote host.
  ansible.posix.synchronize:
    src: /first/absolute/path
    dest: /second/absolute/path
  delegate_to: "{{ inventory_hostname }}"

阅读更多:

<块引用>

https://docs.ansible.com/ansible/latest/collections/ansible/posix/synchronize_module.html

检查:

ansible --version                          
ansible 2.10.5
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/daniel/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  executable location = /sbin/ansible
  python version = 3.9.1 (default, Dec 13 2020, 11:55:53) [GCC 10.2.0]