為 Ansible 設定 SSH 跳轉盒 - 無法連接

為 Ansible 設定 SSH 跳轉盒 - 無法連接

我正在嘗試使用 Ansible 部署到沒有公用 IP 位址的 AWS 主機。我可以透過 ssh 手動連接到跳線盒,然後在跳線盒上 ssh 到私人機器上,例如my machine-->bastion-->private server

我不認為我可以使用 Ansible 原生支持,因為此劇本使用與其他劇本共享的角色。這些角色依賴於特定的庫存組。如果我進行了設置,group_vars那麼將其部署到非 AWS 基礎架構的劇本將會失效。

我的 ssh 設定檔如下所示:

# Servers in availability zone A
    Host 10.0.0.*
      ProxyCommand ssh -W %h:%p bastion.example.com
      IdentityFile ~/.ssh/key.pem

# Servers in availability zone B
Host 10.0.1.*
  ProxyCommand ssh -W %h:%p bastion.example.com
  IdentityFile ~/.ssh/key.pem

# The bastion host itself
Host bastion.example.com
  User ubuntu
  IdentityFile ~/.ssh/key.pem
  ControlMaster auto
  ControlPath ~/.ssh/ansible-%r@%h:%p
  ControlPersist 5m

請注意,堡壘伺服器和私有伺服器的密鑰是相同的。

當我嘗試時,ssh 10.0.0.175 -F /tmp/aws_bastion_ssh_config -vvv我得到以下輸出:

    (venv) andrew@dell:~/projects/ansible-playbooks$ ssh 10.0.0.175 -F /tmp/aws_bastion_ssh_config -vvv
OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /tmp/aws_bastion_ssh_config
debug1: /tmp/aws_bastion_ssh_config line 6: Applying options for 10.0.0.*
debug1: Executing proxy command: exec ssh -W 10.0.0.175:22 bastion.example.com
debug1: permanently_drop_suid: 1000
debug1: key_load_public: No such file or directory
debug1: identity file /home/andrew/.ssh/key.pem type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/andrew/.ssh/key.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
Permission denied (publickey).
ssh_exchange_identification: Connection closed by remote host

我怎樣才能讓它發揮作用?

我需要將鑰匙放在跳線盒上嗎?我該如何配置呢?

ssh bastion.example.com -F /tmp/aws_bastion_ssh_config編輯:更多資訊:- 為了澄清,當我到達堡壘伺服器時,我可以從跳線盒連接到私人電腦。我已將密鑰複製到該伺服器,當我連接到私人電腦時。理想情況下,我不希望將鑰匙放在跳線盒上,我將其放在那裡只是為了確保網路正常運作。ssh [email protected] -i ~/.ssh/key.pem

答案1

您使用 ProxyCommand 所做的就是啟動兩個完全獨立的 ssh 指令。

  1. 嘗試使用代理程式對 10.0.0.175 進行身份驗證,因此您不需要堡壘上的金鑰和配置。
  2. 提供代理的代理,只需要能夠對堡壘進行身份驗證。

您的命令1. 工作正常,但您沒有看到2. 的調試輸出。執行您想要的操作,因為它是從未在您的配置中IdentityFile和選項。User它使用不同的金鑰或使用者進行身份驗證,並被堡壘拒絕(理所當然)。

為了確保 2. 也讀取您的配置,請明確傳遞該選項,如下所示:

# Servers in availability zone A
Host 10.0.0.*
  ProxyCommand ssh -vvv -F /tmp/aws_bastion_ssh_config -W %h:%p bastion.example.com
  IdentityFile ~/.ssh/key.pem

# Servers in availability zone B
Host 10.0.1.*
  ProxyCommand ssh -vvv -F /tmp/aws_bastion_ssh_config -W %h:%p bastion.example.com
  IdentityFile ~/.ssh/key.pem

# The bastion host itself
Host bastion.example.com
  User ubuntu
  IdentityFile ~/.ssh/key.pem
  ControlMaster auto
  ControlPath ~/.ssh/ansible-%r@%h:%p
  ControlPersist 5m

一切都應該有效。確認:

ssh -vvv -F /tmp/aws_bastion_ssh_config 10.0.0.175

然後刪除-vvv表單 1. 和 2 /tmp/aws_bastion_ssh_config-F/etc/ssh/ssh_config ~/.ssh/config files

相關內容