我需要将文件从非清单远程计算机scp
到 Ansible 中的清单中远程计算机。scp
还需要密码才能访问。有没有一种直接的解决方法?
例如:我的库存
hosts:
- node1
- node2
存在文件的计算机:
- gw_machine
在node1
,我想:
scp user@gw_machine:/path/ .
我知道copy
、fetch
和synchronize
模块,但它们似乎不适合我。我尝试编写一个expect
脚本,但是当我第一次连接时它被挂起了,因为gw_machine
不在node1
known_hosts
。
我的期望任务:
- name: Scp file
shell: |
set timeout -1
spawn /usr/bin/scp user@gw_machine:/path_to_file/ .
expect "password:"
send "{{gw_passwd}}n"
expect EOF
exit 0
args:
chdir: /tmp/
executable: /usr/bin/expect
当gw_machine
在已知的node1
主机中时,上述方法有效,但当它不在时挂起。我的机器上没有sshpass
您可以将shell
任务与sshpass
一起使用。 sshpass
将传递密码scp
需要,示例如下:
sshpass -p "<the password here>" scp <username>@<host IP>:<path and file> .
编辑:
如果您想坚持使用现有任务,可以调整 scp
命令以自动将新主机添加到其known_hosts
列表中,方法是添加-oStrictHostKeyChecking=no
:
例如:
[http_offline@greenhat-29 tmp]$ scp -oStrictHostKeyChecking=no /tmp/test1.out localhost:/tmp/test2.out
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
test1.out
100% 0 0.0KB/s 00:00
[http_offline@greenhat-29 tmp]$