Ansibleを使用すると、GCPはCloud Identity Aware Proxy(IAP)の背後にあるホストに接続できますか?

Ansibleを使用すると、GCPはCloud Identity Aware Proxy(IAP)の背後にあるホストに接続できますか?

Cloud IAPは、VPNなしでパブリックIPアドレスなしでコンピューティングインスタンスに接続できるようにするGoogle Cloud Platform用のプロキシです。インスタンスを設定したら、gcloudこのユーティリティを使用して名前でインスタンスに接続できますgcloud compute ssh my-server-01。これはプロキシを介してあなたを認証し、Googleアカウントを使用してターゲットサーバーにログインします(オペレーティングシステムログインと呼ばれる機能を使用)。

gcloudツールが何をするには、カスタム接続プラグインが必要だと思います。

ベストアンサー1

議論後https://www.reddit.com/r/ansible/comments/e9ve5q/ansible_slow_as_a_hell_with_gcp_iap_any_way_to/ソケットを介したSSH接続共有を使用するようにソリューションを変更しました。

@matソリューションより2倍高速です。私たちの製品に入れました。これはホスト名パターンに依存しない実装です!

正しい解決策は Bastion/Jump ホストを使用することです。gcloudコマンドはまだ生成されたPythonインタプリタを生成するためですssh。まだ非効率的です!

ansible.cfg:

[ssh_connection]
pipelining = True
ssh_executable = misc/gssh.sh
ssh_args =
transfer_method = piped

[privilege_escalation]
become = True
become_method = sudo

[defaults]
interpreter_python = /usr/bin/python
gathering = False
# Somehow important to enable parallel execution...
strategy = free

gssh.sh:

#!/bin/bash

# ansible/ansible/lib/ansible/plugins/connection/ssh.py
# exec_command(self, cmd, in_data=None, sudoable=True) calls _build_command(self, binary, *other_args) as:
#   args = (ssh_executable, self.host, cmd)
#   cmd = self._build_command(*args)
# So "host" is next to the last, cmd is the last argument of ssh command.

host="${@: -2: 1}"
cmd="${@: -1: 1}"

# ControlMaster=auto & ControlPath=... speedup Ansible execution 2 times.
socket="/tmp/ansible-ssh-${host}-22-iap"

gcloud_args="
--tunnel-through-iap
--zone=europe-west1-b
--quiet
--no-user-output-enabled
--
-C
-o ControlMaster=auto
-o ControlPersist=20
-o PreferredAuthentications=publickey
-o KbdInteractiveAuthentication=no
-o PasswordAuthentication=no
-o ConnectTimeout=20"

exec gcloud compute ssh "$host" $gcloud_args -o ControlPath="$socket" "$cmd"

修正するgcloudGoogleエンジニアが呼び出すべきではない応答をしました。平行に!バラより'gcloudcomputessh' は並列に使用できません。

実験によると、Ansibleを使用すると、fork=5ほとんど常にエラーが発生します。私はfork=2そのようなことを経験したことがありません。

アップデート2時間の経過とともに、2020年末までにロックエラーなしで並列に実行できましたgcloud compute ssh(WSLでは実行しました)。fork = 10

おすすめ記事