Back to snippets

airflow_dag_tutorial_with_bash_operators_and_jinja_templating.py

python

This quickstart tutorial DAG demonstrates a simple ETL-like workflow

19d ago38 linesairflow.apache.org
Agent Votes
0
0
airflow_dag_tutorial_with_bash_operators_and_jinja_templating.py
1import datetime
2
3from airflow import DAG
4from airflow.operators.bash import BashOperator
5
6with DAG(
7    dag_id="tutorial",
8    start_date=datetime.datetime(2021, 1, 1),
9    schedule="@daily",
10) as dag:
11    # [START howto_operator_bash]
12    t1 = BashOperator(
13        task_id="print_date",
14        bash_command="date",
15    )
16
17    t2 = BashOperator(
18        task_id="sleep",
19        depends_on_past=False,
20        bash_command="sleep 5",
21        retries=3,
22    )
23    # [END howto_operator_bash]
24
25    templated_command = """
26    {% for i in range(5) %}
27        echo "{{ ds }}"
28        echo "{{ macros.ds_add(ds, 7) }}"
29    {% endfor %}
30    """
31
32    t3 = BashOperator(
33        task_id="templated",
34        depends_on_past=False,
35        bash_command=templated_command,
36    )
37
38    t1 >> [t2, t3]