Ansible将归档文件从远程服务器复制并提取到另一个服务器 [英] Ansible copy and extract archived file from remote server to another

查看:946
本文介绍了Ansible将归档文件从远程服务器复制并提取到另一个服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm generating an archived file on remote server A. e.g. /tmp/website.tar.gz and want to transfer/copy and extract the file to the remote server B (/var/www). How do I do that in Ansible?

我的Ansible脚本来创建存档文件:

My Ansible script to create archived file:

- 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模块吗? .我正在尝试使用同步模块将存档文件复制到远程服务器B:

Update: Can I use the rsync module? . I'm trying to use the synchronize module to copy the archived file to the remote server 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?

I have a private key for server B, how to remotely access the server B using private key?

推荐答案

您当前的剧本正在尝试从服务器A到服务器B进行同步,但它没有这样做的权限(如您的错误所示,缺少公共密钥)

Your current playbook is attempting to rsync from server A to server B but it doesn't have permission to do so (lack of public key as your error shows).

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

You can either put server A's public key on server B (if these boxes should be regularly communicating with each other) or you can bring the file back to the Ansible host (which should have access to both servers) and then push it back to server B.

您可以将获取模块用于此类情况.基本上就像复制模块一样,但是相反.

You can use the fetch module for things like this. It's basically like the copy module but in reverse.

示例剧本可能看起来像这样:

An example playbook might look something like this:

- 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

这篇关于Ansible将归档文件从远程服务器复制并提取到另一个服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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