Blog
Wie man Ansible anweist, GCP IAP-Tunneling zu verwenden

Bei einem aktuellen Kunden verwenden wir die IAP-Tunnelfunktion von GCP, um uns mit den VMs zu verbinden.
Bei der Bereitstellung von fluentbit-Paketen auf bestehenden Servern für diesen Kunden beschloss ich, dass ich etwas Zeit sparen würde, wenn ich ein Ansible-Playbook für diese Aufgabe erstellen würde.
Da ich nur über IAP auf die Server zugreifen kann, stieß ich auf ein Problem: Ansible verfügt nicht über die Option, gcloud compute ssh als Verbindungstyp zu verwenden.
Es stellte sich heraus, dass Sie die Möglichkeit haben, die von Ansible verwendete ausführbare ssh-Datei zu überschreiben. Mit Hilfe dieses Beitrags und einigen benutzerdefinierten Änderungen konnte ich mein Playbook auf den GCP-Servern ausführen.
Unten finden Sie die verwendeten Konfigurationen.
ansible.cfg
Inhalt:
[inventory]
enable_plugins = gcp_compute
[defaults]
inventory = misc/inventory.gcp.yml
interpreter_python = /usr/bin/python
[ssh_connection]
# Enabling pipelining reduces the number of SSH operations required
# to execute a module on the remote server.
# This can result in a significant performance improvement
# when enabled.
pipelining = True
scp_if_ssh = False
ssh_executable = misc/gcp-ssh-wrapper.sh
ssh_args = None
# Tell ansible to use SCP for file transfers when connection is set to SSH
scp_if_ssh = True
scp_executable = misc/gcp-scp-wrapper.sh
Zunächst teilen wir Ansible mit, dass wir das gcp_compute Plugin für unser Inventar verwenden möchten.
Dann verweisen wir Ansible auf unsere Inventar-Konfigurationsdatei, deren Inhalt Sie weiter unten finden.
Die ssh_connection-Konfiguration ermöglicht uns die Verwendung von gcloud compute ssh/scp Befehlen für unsere Remote-Verbindungen.
misc/inventory.gcp.yml
Inhalt:
plugin: gcp_compute
projects:
- my-project
auth_kind: application
keyed_groups:
- key: labels
prefix: label
- key: zone
prefix: zone
- key: (tags.items|list)
prefix: tag
groups:
gke : "'gke' in name"
compose:
# set the ansible_host variable to connect with the private IP address without changing the hostname
ansible_host: name
Dies ermöglicht eine automatische Inventarisierung aller Compute-Instanzen, die im my-project GCP-Projekt laufen.
Gruppen werden automatisch auf der Grundlage der angegebenen keyed_groups-Konfiguration erstellt und zusätzlich habe ich eine gke Gruppe auf der Grundlage des VM-Namens hinzugefügt.
Wenn Sie den Ansible_host auf den Namen setzen, wird sichergestellt, dass unser gcloud ssh-Befehl funktioniert. Andernfalls wird Ansible Ihnen die IP-Adresse der Instanz übergeben.
misc/gcp-ssh-wrapper.sh
Inhalt:
#!/bin/bash
# This is a wrapper script allowing to use GCP's IAP SSH option to connect
# to our servers.
# Ansible passes a large number of SSH parameters along with the hostname as the
# second to last argument and the command as the last. We will pop the last two
# arguments off of the list and then pass all of the other SSH flags through
# without modification:
host="${@: -2: 1}"
cmd="${@: -1: 1}"
# Unfortunately ansible has hardcoded ssh options, so we need to filter these out
# It's an ugly hack, but for now we'll only accept the options starting with '--'
declare -a opts
for ssh_arg in "${@: 1: $# -3}" ; do
if [[ "${ssh_arg}" == --* ]] ; then
opts+="${ssh_arg} "
fi
done
exec gcloud compute ssh $opts "${host}" -- -C "${cmd}"
Ansible ruft dieses Skript für alle Remote-Befehle auf, wenn die connection auf ssh
misc/gcp-scp-wrapper.sh
Inhalt:
#!/bin/bash
# This is a wrapper script allowing to use GCP's IAP option to connect
# to our servers.
# Ansible passes a large number of SSH parameters along with the hostname as the
# second to last argument and the command as the last. We will pop the last two
# arguments off of the list and then pass all of the other SSH flags through
# without modification:
host="${@: -2: 1}"
cmd="${@: -1: 1}"
# Unfortunately ansible has hardcoded scp options, so we need to filter these out
# It's an ugly hack, but for now we'll only accept the options starting with '--'
declare -a opts
for scp_arg in "${@: 1: $# -3}" ; do
if [[ "${scp_arg}" == --* ]] ; then
opts+="${scp_arg} "
fi
done
# Remove [] around our host, as gcloud scp doesn't understand this syntax
cmd=`echo "${cmd}" | tr -d []`
exec gcloud compute scp $opts "${host}" "${cmd}"
Dieses Skript wird von Ansible für alle copy Aufgaben aufgerufen, wenn die connection auf ssh
group_vars/all.yml
Inhalt:
---
ansible_ssh_args: --tunnel-through-iap --zone={{ zone }} --no-user-output-enabled --quiet
ansible_scp_extra_args: --tunnel-through-iap --zone={{ zone }} --quiet
Die Übergabe der ssh- und scp-Args durch die group-vars ermöglicht es uns, die Zone auf die Zone der VM zu setzen, die bereits durch das Ansible-Inventar bekannt ist.
Ohne Angabe der Zone gibt gcloud den folgenden Fehler aus:
ERROR: (gcloud.compute.ssh) Underspecified resource [streaming-portal]. Specify the [--zone] flag.
Ich hoffe, dieser Beitrag hilft allen, die das gleiche Problem haben!
Verfasst von

Lotte-Sara Laan
Unsere Ideen
Weitere Blogs
Contact



