Sub topic 6
Variables and per-target overrides
This chapter assumes you can read a databricks.yml with a bundle name, an include list, and two targets, and that you know how a job resource is declared. It covers how one bundle adapts to dev and prod without copying configuration.
Why variables
A target can override workspace and run_as. Many values still differ only slightly between environments: the catalog name, the cluster node type, a schedule. Variables let you write the job once and set each value where it differs.
Variables are declared under the top-level variables key. Each one can have a description and a default. Resources read a variable with the ${var.<name>} substitution.
variables:
catalog:
description: Unity Catalog catalog the job writes to
default: dev_catalog
node_type_id:
description: Cloud-specific node type for job clustersHere catalog has a default. node_type_id has none, so it must receive a value from somewhere else or validation fails.
Using a variable in a resource
The job file refers to both variables with substitutions. The ${var.node_type_id} and ${var.catalog} placeholders are resolved when the bundle is validated and deployed.
resources:
jobs:
daily_etl:
name: daily_etl
job_clusters:
- job_cluster_key: main
new_cluster:
spark_version: 15.4.x-scala2.12
node_type_id: ${var.node_type_id}
num_workers: 2
tasks:
- task_key: ingest
job_cluster_key: main
notebook_task:
notebook_path: ../src/ingest.py
base_parameters:
catalog: ${var.catalog}The notebook receives catalog as a base parameter, so it can read the value with dbutils.widgets.get("catalog").
Overriding per target
A target can contain its own variables block. Values there replace the top-level default for that target only.
targets:
dev:
mode: development
default: true
workspace:
host: https://<your-dev-workspace-url>
variables:
node_type_id: <a-node-type-available-in-your-cloud>
prod:
mode: production
workspace:
host: https://<your-prod-workspace-url>
variables:
catalog: prod_catalog
node_type_id: <a-node-type-available-in-your-cloud>With this configuration:
| Target | `catalog` | `node_type_id` |
|---|---|---|
| dev | `dev_catalog` (top-level default) | value set in `dev` |
| prod | `prod_catalog` (prod override) | value set in `prod` |
Dev never states a catalog, so it inherits the default. Prod states one, so the override wins.
How a value is chosen
When several sources supply a value, the CLI uses the most specific one. A value passed on the command line beats an environment variable, which beats a target override, which beats the top-level default.
You can override a value for one invocation:
databricks bundle deploy -t prod --var="catalog=prod_catalog"An environment variable named BUNDLE_VAR_catalog has the same effect. This is useful in CI, where a pipeline can inject values without editing files.
Common errors
- Missing value. A variable with no default, no target override, and no flag stops
databricks bundle validatewith an error that names the variable. Add a value in the target or pass--var. - Wrong syntax. Write
${var.catalog}, not${catalog}or$var.catalog. Unrecognized text is left as a literal string, which then fails at run time. - Override at the wrong level. A
variablesblock placed underresourcesdoes nothing. Overrides belong at the top level or inside a target. - Cloud-specific values. Node type names differ between clouds. Keeping
node_type_idas a variable avoids editing the job file when you move workspaces.
Practice
Exercise
A bundle declares catalog with default: dev_catalog. The prod target sets catalog: prod_catalog. You run databricks bundle deploy -t prod --var="catalog=staging_catalog". Which catalog does the job use, and why?
Show answer
The job uses staging_catalog. A --var flag has higher precedence than a target override or the top-level default, so it wins for that invocation only.
Exercise
databricks bundle validate -t dev fails because node_type_id has no value. Name two ways to fix it without changing the job resource file.
Show answer
Add node_type_id under variables in the dev target, or give the variable a default at the top level. You can also pass --var="node_type_id=<value>" or set BUNDLE_VAR_node_type_id.