【问题标题】:Anisble scp file from non-inventory node来自非库存节点的 Ansible scp 文件
【发布时间】:2019-04-04 18:56:52
【问题描述】:

我需要将文件从非库存远程机器 scp 到 ansible 中的库存远程机器。 scp 还需要密码才能访问。有没有一种简单的方法可以做到这一点?

例如: 我的库存

hosts:
  - node1
  - node2

文件所在的机器:

  - gw_machine

在 node1 上,我想:

scp user@gw_machine:/path/ .

我知道复制、获取和同步模块,但它们似乎不适合我。我尝试编写一个期望脚本,但是当我第一次连接时它被挂起,因为 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

【问题讨论】:

    标签: ansible devops


    【解决方案1】:

    您可以将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]$
    

    【讨论】:

    • 我可能应该添加一条评论,我在遥控器上没有 sshpass。
    • 这行不通,因为我们对 scp 有密码要求
    • 我建议更新您提供的“预期任务”,因为“当 gw_machine 位于 node1 的已知主机中时,上述内容有效,但不在时挂起。”既然您说问题是当您 scp 到新主机时,请尝试将 -oStrictHostKeyChecking=no 添加到 scp 命令。
    • 知道了。我使用的另一个解决方案是在 ssh 配置中关闭 HostkeyChecking,但这更好。对于阅读本文的其他人,请注意这不是最安全的解决方案。关闭主机密钥检查会使您面临 MITM 攻击。
    猜你喜欢
    • 2015-05-26
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    相关资源
    最近更新 更多