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

查看:130
本文介绍了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?

推荐答案

您当前的 playbook 正在尝试从服务器 A rsync 到服务器 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天全站免登陆