【问题标题】:How to use ansible to run commands through a remote server to another remote server如何使用ansible通过远程服务器向另一台远程服务器运行命令
【发布时间】:2020-06-10 03:59:48
【问题描述】:

在我们的工作设置中,有一个 remote server B 只能通过远程服务器 A 访问。

  • 如何从运行 ansible 的本地系统通过远程服务器 A 在远程服务器 B 上运行 ansible 命令/剧本,即 本地系统 --> 远程服务器 A --> 远程服务器 B
  • remote server B 可以通过 ssh 访问 remote server A。但我无权访问remote server B 的 ssh 密钥

这是我根据下面的答案在我的 inventory.yaml 文件中尝试做的事情

  hosts:
    remote-serverB:
  vars:
    ansible_connection: "ssh"
    ansible_user: "userB"
    ansible_ssh_common_args: '-o ProxyCommand="sshpass -p <password> ssh -W %h:%p -q userA@remote-serverA"' 

但我从 ansible 得到以下错误

UNREACHABLE  {"changed": false, "msg": "EOF on stream; last 100 lines received:\nssh_exchange_identification: Connection closed by remote host\r", "unreachable": true}

【问题讨论】:

    标签: ansible


    【解决方案1】:

    这可以通过ansible_ssh_common_args 完成。

    ansible_ssh_common_args
    此设置始终附加到 sftp、scp 和 ssh 的默认命令行。对于为某个主机(或组)配置ProxyCommand 很有用。

    来源:https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#connecting-to-hosts-behavioral-inventory-parameters

    这是一个使用此配置的清单示例,假设我的两台服务器具有 FQDN

    • remote-server-A
    • remote-server-B

    并且登录remote-server-A的用户是user-to-A

    all:
      servers-needing-jump:
        hosts:
          remote-server-B:
      vars:
        ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q user-to-A@remote-server-A"'
    

    【讨论】:

    • 感谢@β.εηοιτ.βε 的回复,但我收到错误消息。我已经用我尝试过的方法更新了我上面的问题。你能告诉我我可能做错了什么吗?谢谢
    猜你喜欢
    • 2019-05-19
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 2021-09-21
    • 2017-03-29
    相关资源
    最近更新 更多