无法使用Ansible同步模块复制远程到远程

时间:2018-07-06 08:17:32

标签: ansible

我正在尝试将目录从服务器a复制到服务器b。

这是我的文件:

test.yml

- hosts: server-b  
  tasks:
   - name: Copy Remote-To-Remote (from server-a to server-b)
     synchronize: src=/test-dir dest=/test-dir
     delegate_to: server-a

hosts.yml

all:
  hosts:
    server-a:
      ansible_host: 123.123.123.101
      ansible_user: root
    server-b:
      ansible_host: 123.123.123.102
      ansible_user: root

ansible.cfg

[defaults]
inventory = hosts.yml
host_key_checking = false

此处输出:

$ ansible-playbook test.yml
PLAY [server-b] *******************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************
********************************************
ok: [server-b]

TASK [Copy Remote-To-Remote (from server-a to server-b)] **************************************************************************************

因此,它挂起并且不复制任何内容。

这是详细的日志(带有-vvvv):

TASK [Copy Remote-To-Remote (from server-a to server-b)] **************************************************************************************
task path: /Users/max7z/projects/test/t11__ansible__copy_dir_from_server-a_to_server-b/test.yml:3
<server-a-ip> ESTABLISH SSH CONNECTION FOR USER: root
<server-a-ip> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/max7z/.ansible/cp/9c5eb423c5 server-a-ip '/bin/sh -c '"'"'echo ~root && sleep 0'"'"''
<server-a-ip> (0, '/root\n', 'OpenSSH_7.6p1, LibreSSL 2.6.2\r\ndebug1: Reading configuration data /Users/max7z/.ssh/config\r\ndebug1: /Users/max7z/.ssh/config line 1: Applying options for *\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 48: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 64910\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 0\r\n')
<server-a-ip> ESTABLISH SSH CONNECTION FOR USER: root
<server-a-ip> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/max7z/.ansible/cp/9c5eb423c5 server-a-ip '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1530864809.51-156163648940516 `" && echo ansible-tmp-1530864809.51-156163648940516="` echo /root/.ansible/tmp/ansible-tmp-1530864809.51-156163648940516 `" ) && sleep 0'"'"''
<server-a-ip> (0, 'ansible-tmp-1530864809.51-156163648940516=/root/.ansible/tmp/ansible-tmp-1530864809.51-156163648940516\n', 'OpenSSH_7.6p1, LibreSSL 2.6.2\r\ndebug1: Reading configuration data /Users/max7z/.ssh/config\r\ndebug1: /Users/max7z/.ssh/config line 1: Applying options for *\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 48: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 64910\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 0\r\n')
Using module file /usr/local/Cellar/ansible/2.6.0/libexec/lib/python2.7/site-packages/ansible/modules/files/synchronize.py
<server-a-ip> PUT /Users/max7z/.ansible/tmp/ansible-local-65309JwJs2q/tmpBWQj1p TO /root/.ansible/tmp/ansible-tmp-1530864809.51-156163648940516/synchronize.py
<server-a-ip> SSH: EXEC sftp -b - -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/max7z/.ansible/cp/9c5eb423c5 '[server-a-ip]'
<server-a-ip> (0, 'sftp> put /Users/max7z/.ansible/tmp/ansible-local-65309JwJs2q/tmpBWQj1p /root/.ansible/tmp/ansible-tmp-1530864809.51-156163648940516/synchronize.py\n', 'OpenSSH_7.6p1, LibreSSL 2.6.2\r\ndebug1: Reading configuration data /Users/max7z/.ssh/config\r\ndebug1: /Users/max7z/.ssh/config line 1: Applying options for *\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 48: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 64910\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug2: Remote version: 3\r\ndebug2: Server supports extension "posix-rename@openssh.com" revision 1\r\ndebug2: Server supports extension "statvfs@openssh.com" revision 2\r\ndebug2: Server supports extension "fstatvfs@openssh.com" revision 2\r\ndebug2: Server supports extension "hardlink@openssh.com" revision 1\r\ndebug2: Server supports extension "fsync@openssh.com" revision 1\r\ndebug3: Sent message fd 8 T:16 I:1\r\ndebug3: SSH_FXP_REALPATH . -> /root size 0\r\ndebug3: Looking up /Users/max7z/.ansible/tmp/ansible-local-65309JwJs2q/tmpBWQj1p\r\ndebug3: Sent message fd 8 T:17 I:2\r\ndebug3: Received stat reply T:101 I:2\r\ndebug1: Couldn\'t stat remote file: No such file or directory\r\ndebug3: Sent message SSH2_FXP_OPEN I:3 P:/root/.ansible/tmp/ansible-tmp-1530864809.51-156163648940516/synchronize.py\r\ndebug3: Sent message SSH2_FXP_WRITE I:4 O:0 S:32768\r\ndebug3: SSH2_FXP_STATUS 0\r\ndebug3: In write loop, ack for 4 32768 bytes at 0\r\ndebug3: Sent message SSH2_FXP_WRITE I:5 O:32768 S:32768\r\ndebug3: Sent message SSH2_FXP_WRITE I:6 O:65536 S:6565\r\ndebug3: SSH2_FXP_STATUS 0\r\ndebug3: In write loop, ack for 5 32768 bytes at 32768\r\ndebug3: SSH2_FXP_STATUS 0\r\ndebug3: In write loop, ack for 6 6565 bytes at 65536\r\ndebug3: Sent message SSH2_FXP_CLOSE I:4\r\ndebug3: SSH2_FXP_STATUS 0\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 0\r\n')
<server-a-ip> ESTABLISH SSH CONNECTION FOR USER: root
<server-a-ip> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/max7z/.ansible/cp/9c5eb423c5 server-a-ip '/bin/sh -c '"'"'chmod u+x /root/.ansible/tmp/ansible-tmp-1530864809.51-156163648940516/ /root/.ansible/tmp/ansible-tmp-1530864809.51-156163648940516/synchronize.py && sleep 0'"'"''
<server-a-ip> (0, '', 'OpenSSH_7.6p1, LibreSSL 2.6.2\r\ndebug1: Reading configuration data /Users/max7z/.ssh/config\r\ndebug1: /Users/max7z/.ssh/config line 1: Applying options for *\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 48: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 64910\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 0\r\n')
<server-a-ip> ESTABLISH SSH CONNECTION FOR USER: root
<server-a-ip> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/max7z/.ansible/cp/9c5eb423c5 -tt server-a-ip '/bin/sh -c '"'"'/usr/bin/python /root/.ansible/tmp/ansible-tmp-1530864809.51-156163648940516/synchronize.py && sleep 0'"'"''

为什么不远程复制?

1 个答案:

答案 0 :(得分:0)

答案是,ansible模块“正在运行,并且起源于正在运行Ansible的本地主机。”。参见module documentation。 要复制远程复制,您必须交换ssh键。您可以将以下标语用作示例:

- hosts: server-a
  tasks:
    # transfer local pub-key to remote authorized_keys
    - name: fetch local ssh key from root user
      shell: cat /root/.ssh/id_rsa.pub
      register: ssh_keys
      changed_when: false
    - name: deploy ssh key to remote server
      authorized_key:
              user: "root"
              key: "{{ item }}"
      delegate_to: "server-b"
      with_items:
              - "{{ ssh_keys.stdout }}"
    # fetch remote host key and add to local known_hosts
    # to omit key accept prompt
    - name: fetch ssh rsa host key from remote server
      shell: cat /etc/ssh/ssh_host_rsa_key.pub
      register: ssh_host_rsa_key
      delegate_to: server-b
      changed_when: false
    - name: create /root/.ssh/ if not existant
      file:
          path: "/root/.ssh/"
          owner: root
          group: root
          mode: 0700
          state: directory
    - name: add hostkey to root known host file
      lineinfile:
          path: "/root/.ssh/known_hosts"
          line: "{{ server-b.inventory_hostname }} {{ ssh_host_rsa_key.stdout }}"
          mode: 0600