假设ansible在主机1上运行,如何将文件从主机2复制到主机3
我已经在主机1上安装了ansible。主机2上有一个文件,我需要ansible将它复制到主机3上。我正在使用RHEL 我在host2上运行了下面的yml,但是它在复制文件任务中被卡住了假设ansible在主机1上运行,如何将文件从主机2复制到主机3,ansible,Ansible,我已经在主机1上安装了ansible。主机2上有一个文件,我需要ansible将它复制到主机3上。我正在使用RHEL 我在host2上运行了下面的yml,但是它在复制文件任务中被卡住了 - name: Ensure sshd allows agent forwarding lineinfile: dest=/etc/ssh/sshd_config regexp=^#?AllowAgentForwarding line="AllowAge
- 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
谁能解释一下这里少了什么。如果可以,请提供有关正确主机的详细步骤。问题很可能是您无法从主机2登录到主机3,并且Ansible正在挂起此任务,因为rsync正在等待您输入ssh密码。您能够从主机2手动登录到主机3吗 我之前在中回答了相同的问题(无法标记为重复,因为没有接受答案…) 以下是链接答案的一个稍加修改的版本。这些任务是为CentOS编写的,因此应该可以在RHEL上运行
为此,您需要将私有ssh密钥部署到host2,或者最好启用ssh代理转发,例如在
.ssh/config
中:
Host host2
ForwardAgent yes
另外,主机2上的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 strichostkeychecking=no
来绕过主机密钥检查。您可以将文件发送到主机1,然后再从主机1发送到主机3吗?或者,如果文件很大,请将其发送到AWS S3,然后从host3复制?