GCP_PROJECT_ID 與專案的 Project ID 搭配使用,其中包含執行 DAG 的 VM 和環境。
importdatetimeimportairflowfromairflow.providers.ssh.operators.sshimportSSHOperatorfromairflow.providers.google.cloud.hooks.compute_sshimportComputeEngineSSHHookGCE_INSTANCE='example-compute-instance'GCE_ZONE='us-central1-a'GCP_PROJECT_ID='example-project'withairflow.DAG('composer_compute_ssh_dag',start_date=datetime.datetime(2022,1,1))asdag:ssh_task=SSHOperator(task_id='composer_compute_ssh_task',ssh_hook=ComputeEngineSSHHook(instance_name=GCE_INSTANCE,zone=GCE_ZONE,project_id=GCP_PROJECT_ID,use_oslogin=True,use_iap_tunnel=False,use_internal_ip=True),command='echo This command is executed from a DAG',dag=dag)
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-15 (世界標準時間)。"],[[["This guide explains how to establish a connection from a Directed Acyclic Graph (DAG) to a Compute Engine Virtual Machine (VM) instance, a feature available exclusively in Airflow 2."],["Utilize `ComputeEngineSSHHook` within the `ssh_hook` parameter of `SSHOperator` to configure the connection, ensuring the parameters align with the target Compute Engine VM."],["The example provided demonstrates using `SSHOperator` to execute commands on a specified Compute Engine VM instance, requiring replacement of placeholder values like `GCE_INSTANCE`, `GCE_ZONE`, and `GCP_PROJECT_ID` with actual values."],["The example code showcases how to define an SSH task that will connect to the compute engine VM and run a command."]]],[]]