Ansible:来自非清单节点的 scp 文件



我需要将文件从非清单远程计算机scp到 Ansible 中的清单中远程计算机。scp还需要密码才能访问。有没有一种直接的解决方法?

例如:我的库存

hosts:
  - node1
  - node2

存在文件的计算机:

  - gw_machine

node1,我想:

scp user@gw_machine:/path/ .

我知道copyfetchsynchronize模块,但它们似乎不适合我。我尝试编写一个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]$

最新更新