Ansible将存档文件从远程服务器复制并提取到另一个

时间:2015-10-29 03:17:11

标签: ansible tar

我正在远程服务器A上生成一个存档文件。 /tmp/website.tar.gz 并希望将文件传输/复制并解压缩到远程服务器B(/ var / www)。 我如何在Ansible中做到这一点?

用于创建存档文件的My Ansible脚本:

- name: archive files
  shell: tar -zczf /tmp/website.tar.gz .
  args:
    chdir: "{{ source_dir }}"
  tags: release

- name: copy archived file to another remote server and unpack to directory /var/www. unresolved..

更新: 我可以使用rsync module吗? 。我正在尝试使用synchronize模块将存档文件复制到远程服务器B:

- name: copy archived file to another remote server and unpack to directory /var/www
  synchronize:
    src: /tmp/website.tar.gz
    dest: /var/www
    rsync_opts:
      - "--rsh=ssh -i /home/agan/key/privatekey"
  delegate_to: remote_server_b
  tags: test

它会产生错误:

fatal: [remote_server_b] => SSH Error: Permission denied (publickey).
    while connecting to xxx.xxx.xxx.129:22
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.

FATAL: all hosts have already failed -- aborting

我有一个服务器B的私钥,如何使用私钥远程访问服务器B?

2 个答案:

答案 0 :(得分:2)

您当前的Playbook正在尝试从服务器A到服务器B的rsync,但它没有这样做的权限(错误显示缺少公钥)。

您可以将服务器A的公钥放在服务器B上(如果这些框应该定期相互通信),或者您可以将文件带回Ansible主机(应该可以访问两台服务器),然后推送它回到服务器B。

你可以使用fetch module来做这样的事情。它基本上与copy module相似,但相反。

示例剧本可能如下所示:

- hosts: serverA
  tasks:
    - name: archive files on serverA
      shell: tar -czf /tmp/website.tar.gz .
      args:
        chdir: "{{ source_dir }}"
      tags: release
    - name: fetch the archive from serverA
      fetch:
        src: /tmp/website.tar.gz
        dest: /tmp/website.tar.gz

- hosts: serverB
  tasks:
    - name: copy the archive to serverB
      copy:
        src: /tmp/website.tar.gz
        dest: /tmp/website.tar.gz

答案 1 :(得分:1)

- name: copy archived file to another remote server and using rsync
  synchronize: mode=pull src=/home/{{ user }}/packages/{{ package_name }}.tar.gz dest=/tmp/{{ package_name }}.tar.gz archive=yes
  delegate_to: "{{ production_server }}"
  tags: release