Variables

Variables are Airflow’s runtime configuration concept - a general key/value store that is global and can be queried from your tasks, and easily set via Airflow’s user interface, or bulk-uploaded as a JSON file.

To use them, just import and call get on the Variable model:

  1. from airflow.models import Variable
  2. # Normal call style
  3. foo = Variable.get("foo")
  4. # Auto-deserializes a JSON value
  5. bar = Variable.get("bar", deserialize_json=True)
  6. # Returns the value of default_var (None) if the variable is not set
  7. baz = Variable.get("baz", default_var=None)

You can also use them from templates:

  1. # Raw value
  2. echo {{ var.value.<variable_name> }}
  3. # Auto-deserialize JSON value
  4. echo {{ var.json.<variable_name> }}

Variables are global, and should only be used for overall configuration that covers the entire installation; to pass data from one Task/Operator to another, you should use XComs instead.

We also recommend that you try to keep most of your settings and configuration in your DAG files, so it can be versioned using source control; Variables are really only for values that are truly runtime-dependent.

For more information on setting and managing variables, see Managing Variables.