从 BashOperator 到 SSHOperator 的 Airflow XCOM 通信

3Le*_*exW 5 python airflow airflow-2.x

我刚刚开始学习Airflow,但是掌握Xcom的概念是相当困难的。因此我写了这样的达格:

from airflow import DAG
from airflow.utils.edgemodifier import Label

from datetime import datetime
from datetime import timedelta

from airflow.operators.bash import BashOperator
from airflow.contrib.operators.ssh_operator import SSHOperator
from airflow.contrib.hooks.ssh_hook import SSHHook

#For more default argument for a task (or creating templates), please check this website
#https://airflow.apache.org/docs/apache-airflow/stable/_api/airflow/models/index.html#airflow.models.BaseOperator

default_args = {
    'owner': '...',
    'email': ['...'],
    'email_on_retry': False,
    'email_on_failure': True,
    'retries': 3,
    'retry_delay': timedelta(minutes=5),
    'start_date': datetime(2021, 6, 10, 23, 0, 0, 0),
    
}

hook = SSHHook(
    remote_host='...',
    username='...',
    password='...## Heading ##',
    port=22,
)

with DAG(
    'test_dag',
    description='This is my first DAG to learn BASH operation, SSH connection, and transfer data among jobs',
    default_args=default_args,
    start_date=datetime(2021, 6, 10, 23, 0, 0, 0),
    schedule_interval="0 * * * *",
    tags = ['Testing', 'Tutorial'],
) as dag:
    # Declare Tasks
    Read_my_IP = BashOperator(
        # Task ID has to be the combination of alphanumeric chars, dashes, dots, and underscores 
        task_id='Read_my_IP',
        # The last line will be pushed to next task
        bash_command="hostname -i | awk '{print $1}'",
    )

    Read_remote_IP = SSHOperator(
        task_id='Read_remote_IP',
        ssh_hook=hook,
        environment={
            'Pi_IP': Read_my_IP.xcom_pull('Read_my_IP'),
        },
        command="echo {{Pi_IP}}",
    )

    # Declare Relationship between tasks
    Read_my_IP >> Label("PI's IP address") >> Read_remote_IP
Run Code Online (Sandbox Code Playgroud)

第一个任务运行成功,但我无法从任务中获取 XCom return_value Read_my_IP,即本地计算机的 IP 地址。这可能是非常基本的,但文档没有提到如何声明task_instance.

请帮助完成 Xcom 流程并将 IP 地址从本地计算机传递到远程计算机以进行进一步的操作。

Ela*_*lad 4

command的参数是SSHOperator模板化的,因此可以直接获取xcom:

Read_remote_IP = SSHOperator(
    task_id='Read_remote_IP',
    ssh_hook=hook,
    command="echo {{ ti.xcom_pull(task_ids='Read_my_IP') }}"
)
Run Code Online (Sandbox Code Playgroud)

请注意,您还需要明确请求从 xcom 推送BashOperator(请参阅操作员描述):

Read_my_IP = BashOperator(
    # Task ID has to be the combination of alphanumeric chars, dashes, dots, and underscores 
    task_id='Read_my_IP',
    # The last line will be pushed to next task
    bash_command="hostname -i | awk '{print $1}'",
    do_xcom_push=True
)
Run Code Online (Sandbox Code Playgroud)