Ansible - 通过带有 MFA 的堡垒访问

Pau*_*rby 9 configuration-management two-factor ansible bastion

在我当前的环境中,我的所有 Linux 服务器只能通过启用 MFA 的堡垒主机访问。

我已经设法让 Ansible 通过堡垒成功地与服务器通信,唯一的问题是它为每个主机建立了一个到堡垒的新连接,这意味着我必须输入与服务器一样多的 MFA 密钥。艰难时期。:(

我试过在我的 ssh 配置中搞乱这样的东西,试图让多路复用工作:

Host bastion
  ControlMaster auto
  ControlPath ~/.ssh/ansible-%r@%h:%p
  ControlPersist 5m
Run Code Online (Sandbox Code Playgroud)

不幸的是,它似乎并没有这样做。任何人都知道如何阻止 Ansible 通过我的堡垒主机为它接触的每个主机重新建立连接?

谢谢!

Hen*_*gel 1

我刚刚偶然发现了这篇关于使用堡垒主机运行 Ansible 的博客文章

显然您需要将堡垒主机添加到控制主机ssh_config

Host 10.10.10.*
  ProxyCommand ssh -W %h:%p bastion.example.com
  IdentityFile ~/.ssh/private_key.pem

Host bastion.example.com
  Hostname bastion.example.com
  User ubuntu
  IdentityFile ~/.ssh/private_key.pem
  ControlMaster auto
  ControlPath ~/.ssh/ansible-%r@%h:%p
  ControlPersist 5m
Run Code Online (Sandbox Code Playgroud)

编辑ssh_argsansible.cfg

[ssh_connection]
ssh_args = -F ./ssh.cfg -o ControlMaster=auto -o ControlPersist=30m control_path = ~/.ssh/ansible-%%r@%%h:%%p
Run Code Online (Sandbox Code Playgroud)

这应该覆盖bastion配置的一部分。对于这个github 问题MFA中的部分用户来说,可以在 Ansible 中使用在 Ansible 外部打开的 ssh 会话。

我打开与具有 2FA 的主机的初始连接,然后在另一个窗口中运行如下命令:

ansible-playbook thing.yml --ssh-common-args='-o ControlPath=~/.ssh/connshare'
Run Code Online (Sandbox Code Playgroud)

我手头没有堡垒主机设置,但我认为这个策略值得一试。