使用Ansible将tar球从一台服务器复制到另一台服务器

时间:2016-12-08 09:42:42

标签: ansible rsync

我正在尝试使用tar.gzAnsible存档文件从控制主机复制到远程服务器。这是戏剧的样子:

 # 0. Copy tar ball into directory.
    - name: copy java tar ball
      tags: copyJavaTarBall
      register: copyJavaTarBallOut
      copy: src={{ javaSrcLocation }} dest={{ javaDestLocation }}
      #shell: /bin/rsync -v -e "ssh -p {{ ansible_port }} -i {{ ansible_ssh_private_key_file }}" {{ javaSrcLocation }} sriram@{{ ansible_host }}:/home/sriram
      #synchronize:
      #  src: {{ javaSrcLocation }}
      #  dest: {{ javaDestLocation }}
      #  rsync_opts:
          #- "-e \" ssh -p {{ ansible_port }} -i {{ ansible_ssh_private_key_file }} \""
      #delegate_to: localhost
    - debug: var=copyJavaTarBallOut.stdout_lines

从所有这些中,copy指令仅适用于简单文件(文本文件等)。使用tar.gz存档会导致以下错误:

task path: /root/ansible/playbooks/javaSetup.yml:22
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 119, in run
    res = self._execute()
  File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 490, in _execute
    result = self._handler.run(task_vars=variables)
  File "/usr/lib/python2.7/site-packages/ansible/plugins/action/copy.py", line 153, in run
    source_full = self._loader.get_real_file(source_full)
  File "/usr/lib/python2.7/site-packages/ansible/parsing/dataloader.py", line 402, in get_real_file
    if is_encrypted_file(f):
  File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 152, in is_encrypted_file
    b_vaulttext = to_bytes(to_text(vaulttext, encoding='ascii', errors='strict'), encoding='ascii', errors='strict')
  File "/usr/lib/python2.7/site-packages/ansible/module_utils/_text.py", line 177, in to_text
    return obj.decode(encoding, errors)
MemoryError

fatal: [xx.yy.zz.aa]: FAILED! => {
    "failed": true,
    "msg": "Unexpected failure during module execution.",
    "stdout": ""
}

rsyncsynchronizeshell指令一起使用会导致执行停滞。

我的问题:
1.如何使用Ansible将tar球从控制主机复制到目标服务器?

**更新:
- Ansible被这样调用:
ANSIBLE_KEEP_REMOTE_FILES=1 ansible-playbook -vvvv -s javaSetup.yml --key-file=sshPvtKeys/pvtKeyFile.pem -c ssh --ask-sudo-pass -l appservers --tags copyJavaTarBall
- 文件大小为147M。 RAM大小约为1 GB。

1 个答案:

答案 0 :(得分:0)

Ansible用于复制大尺寸文件时,似乎存在内存问题。有一个relevant discussion here

我采取的解决这个问题的步骤:
1.使用split将存档分成多个部分,如下所示:
split -b 10m jdk-7u79-linux-x64.tar.gz jdk-7u79-linux-x64.tar.gz.part-
这出戏看起来像这样:

 # 0. Copy tar ball into directory.
    - name: copy java tar ball
      tags: copyJavaTarBall
      register: copyJavaTarBallOut
      copy: src={{ item }} dest={{ javaDestLocation }}
      with_fileglob:
        - "{{ javaSrcLocation }}/*"
    - debug: var=copyJavaTarBallOut.stdout_lines

将其移至远程服务器后,需要将其合并(使用cat),然后取消归档。繁琐,但完成工作。