【问题标题】:How to copy file from host2 to host3 assuming ansible was run on host1假设ansible在host1上运行,如何将文件从host2复制到host3
【发布时间】:2015-08-13 06:51:51
【问题描述】:

我在 host1 上安装了 ansible。 host2 上有文件,我需要通过 ansible 将其复制到 host3 上。我正在使用 RHEL。

我在 host2 上运行了以下 yml,但它卡在了复制文件任务中。

- name: Ensure sshd allows agent forwarding
  lineinfile: dest=/etc/ssh/sshd_config
              regexp=^#?AllowAgentForwarding
              line="AllowAgentForwarding yes"
              follow=yes
              backup=yes
  sudo: yes
  register: changed_sshd_config

- name: "RHEL: Restart sshd"
  shell: /etc/init.d/sshd restart
  sudo: yes
  when:
    - changed_sshd_config | changed

- name: Copy file from host2 to host3
  shell: rsync -r -v -e ssh /root/samplefile.gz root@host3:/root

谁能解释一下这里缺少什么。如果可以,请提供提及正确主机的详细步骤。

【问题讨论】:

  • 你能把文件发送到host1,然后从那里发送到host3吗?或者如果文件太大,请将其发送到AWS S3,然后从 host3 复制?

标签: ansible


【解决方案1】:

问题很可能是您无法从 host2 登录到 host3 并且 Ansible 在此任务上挂起,因为 rsync 正在等待您输入 ssh 密码。可以手动从host2登录到host3吗?

我之前在How communicate two remote machine through ansible 中回答了同样的问题(不能标记为重复,因为没有接受任何答案...)

以下是链接答案的略微修改版本。这些任务是为 CentOS 编写的,所以它应该可以在 RHEL 上运行。


为此,您需要将私有 ssh 密钥部署到 host2,或者最好启用 ssh 代理转发,例如在您的 .ssh/config

Host host2
    ForwardAgent yes

另外,host2 上的 sshd 需要接受代理转发。以下是我用来执行此操作的一些任务:

- name: Ensure sshd allows agent forwarding
  lineinfile: dest=/etc/ssh/sshd_config
              regexp=^#?AllowAgentForwarding
              line="AllowAgentForwarding yes"
              follow=yes
              backup=yes
  sudo: yes
  register: changed_sshd_config

- name: "Restart sshd"
  shell: systemctl restart sshd.service
  sudo: yes
  when: changed_sshd_config | changed

您可能需要在单独的剧本中进行配置。因为 Ansible 可能会保持与主机的开放 ssh 连接,并且在激活代理转发后,您可能需要重新连接。

【讨论】:

  • Rsync 也可能在主机密钥检查中挂起,您可以通过在 ssh 命令行中添加 -o StrictHostKeyChecking=no 来绕过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 2017-05-02
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 2021-06-23
相关资源
最近更新 更多