Macros reference¶
Variables and macros can be used in templates (see the Jinja Templating section)
The following come for free out of the box with Airflow.
Additional custom macros can be added globally through ORM Extensions, or at a DAG level through the DAG.user_defined_macros
argument.
Default Variables¶
The Airflow engine passes a few variables by default that are accessible in all templates
Variable |
Description |
---|---|
|
the execution date as |
|
the execution date as |
|
the previous execution date as |
|
the previous execution date as |
|
the next execution date as |
|
the next execution date as |
|
the day before the execution date as |
|
the day before the execution date as |
|
the day after the execution date as |
|
the day after the execution date as |
|
same as |
|
same as |
|
same as |
|
the execution_date (pendulum.Pendulum) |
|
the previous execution date (if available) (pendulum.Pendulum) |
|
the next execution date (pendulum.Pendulum) |
|
the DAG object |
|
the Task object |
|
a reference to the macros package, described below |
|
the task_instance object |
|
same as |
|
same as |
|
same as |
|
a reference to the user-defined params dictionary which can be overridden by
the dictionary passed through |
|
global defined variables represented as a dictionary |
|
global defined variables represented as a dictionary with deserialized JSON object, append the path to the key within the JSON object |
|
a unique, human-readable key to the task instance
formatted |
|
the full configuration object located at
|
|
the |
|
a reference to the DagRun object |
|
whether the task instance was called using the CLI’s test subcommand |
Note that you can access the object’s attributes and methods with simple
dot notation. Here are some examples of what is possible:
{{ task.owner }}
, {{ task.task_id }}
, {{ ti.hostname }}
, …
Refer to the models documentation for more information on the objects’
attributes and methods.
The var
template variable allows you to access variables defined in Airflow’s
UI. You can access them as either plain-text or JSON. If you use JSON, you are
also able to walk nested structures, such as dictionaries like:
{{ var.json.my_dict_var.key1 }}
Macros¶
Macros are a way to expose objects to your templates and live under the
macros
namespace in your templates.
A few commonly used libraries and methods are made available.
Variable |
Description |
---|---|
|
The standard lib’s |
|
The standard lib’s |
|
A reference to the |
|
The standard lib’s |
|
The standard lib’s |
|
The standard lib’s |
Some airflow specific macros are also defined:
-
airflow.macros.
ds_add
(ds, days)[source]¶ Add or subtract days from a YYYY-MM-DD
- Parameters
>>> ds_add('2015-01-01', 5) '2015-01-06' >>> ds_add('2015-01-06', -5) '2015-01-01'
-
airflow.macros.
ds_format
(ds, input_format, output_format)[source]¶ Takes an input string and outputs another string as specified in the output format
- Parameters
>>> ds_format('2015-01-01', "%Y-%m-%d", "%m-%d-%y") '01-01-15' >>> ds_format('1/5/2015', "%m/%d/%Y", "%Y-%m-%d") '2015-01-05'
-
airflow.macros.
random
() → x in the interval [0, 1).¶
-
airflow.macros.hive.
closest_ds_partition
(table, ds, before=True, schema='default', metastore_conn_id='metastore_default')[source]¶ This function finds the date in a list closest to the target date. An optional parameter can be given to get the closest before or after.
- Parameters
table (str) – A hive table name
ds (list[datetime.date]) – A datestamp
%Y-%m-%d
e.g.yyyy-mm-dd
before (bool or None) – closest before (True), after (False) or either side of ds
- Returns
The closest date
- Return type
>>> tbl = 'airflow.static_babynames_partitioned' >>> closest_ds_partition(tbl, '2015-01-02') '2015-01-01'
-
airflow.macros.hive.
max_partition
(table, schema='default', field=None, filter_map=None, metastore_conn_id='metastore_default')[source]¶ Gets the max partition for a table.
- Parameters
schema (str) – The hive schema the table lives in
table (str) – The hive table you are interested in, supports the dot notation as in “my_database.my_table”, if a dot is found, the schema param is disregarded
metastore_conn_id (str) – The hive connection you are interested in. If your default is set you don’t need to use this parameter.
filter_map (map) – partition_key:partition_value map used for partition filtering, e.g. {‘key1’: ‘value1’, ‘key2’: ‘value2’}. Only partitions matching all partition_key:partition_value pairs will be considered as candidates of max partition.
field (str) – the field to get the max value from. If there’s only one partition field, this will be inferred
>>> max_partition('airflow.static_babynames_partitioned') '2015-01-01'