Sub topic 4
Profiles and environment variables
This chapter covers how the Databricks CLI, SDK, and bundles decide which workspace and credentials to use. It assumes you can already run databricks auth login (previous chapter), you are comfortable with shell environment variables, and you know what a workspace host URL is.
Why you need more than one set of credentials
Most teams work with more than one workspace: a development workspace, a staging workspace, and production. You may also have both a personal login and a service principal (a non-human identity for automation). Typing --host and a token on every command is slow and error-prone. Worse, it makes it easy to run a command against the wrong workspace.
Databricks gives you two mechanisms for this:
- Configuration profiles: named sections in a file,
~/.databrickscfg, that hold a host and credentials. - Environment variables: values such as
DATABRICKS_HOSTthat tools read at startup.
Both are understood by the CLI, the Python SDK, and Asset Bundles, so one setup serves every tool in this tutorial.
The `~/.databrickscfg` file
The configuration file is plain INI text in your home directory. Each section header in square brackets is a profile name. Keys inside the section hold the settings.
[DEFAULT]
host = https://dev-workspace.cloud.databricks.com
auth_type = databricks-cli
[staging]
host = https://staging-workspace.cloud.databricks.com
token = dapiXXXXXXXXXXXXXXXXXXXX
[prod-sp]
host = https://prod-workspace.cloud.databricks.com
client_id = 11111111-2222-3333-4444-555555555555
client_secret = doseXXXXXXXXXXXXXXXXXXXXThe three profiles use three different authentication methods:
| Profile | Method | Keys |
|---|---|---|
| `DEFAULT` | OAuth login as a user | `host`, `auth_type = databricks-cli` |
| `staging` | Personal access token | `host`, `token` |
| `prod-sp` | OAuth machine-to-machine (service principal) | `host`, `client_id`, `client_secret` |
The profile named DEFAULT is special. When you do not choose a profile, tools use DEFAULT.
Creating profiles with `databricks auth login`
You rarely need to edit the file by hand. Running login with a profile name writes the section for you:
databricks auth login --host https://staging-workspace.cloud.databricks.com --profile staging
databricks auth profilesdatabricks auth profiles lists every profile and whether its credentials currently validate. Use it first when something fails, because it tells you whether the problem is the profile or your command.
With OAuth login, the file stores no secret. The short-lived tokens are cached separately under ~/.databricks/token-cache.json and refreshed when needed. A profile with a personal access token or a client secret does store the secret in plain text, so protect the file (see the security callout below).
Selecting a profile
You can select a profile in three ways.
- Pass
--profile(or-p) on a command. - Set the
DATABRICKS_CONFIG_PROFILEenvironment variable. - Do nothing, and let the tool use
DEFAULT.
databricks jobs list --profile staging
export DATABRICKS_CONFIG_PROFILE=prod-sp
databricks current-user meThe current-user me command is a cheap safety check. It prints the identity the CLI is acting as. Run it before any destructive command in a shell where you are not sure which profile is active.
Environment variables
Environment variables are the usual choice for automation, where there is no home directory with a config file. The most common ones are:
| Variable | Meaning |
|---|---|
| `DATABRICKS_HOST` | Workspace URL, for example `https://dev-workspace.cloud.databricks.com` |
| `DATABRICKS_TOKEN` | Personal access token |
| `DATABRICKS_CLIENT_ID` | Service principal application ID (OAuth M2M) |
| `DATABRICKS_CLIENT_SECRET` | Service principal OAuth secret |
| `DATABRICKS_CONFIG_PROFILE` | Name of the profile to use |
| `DATABRICKS_CONFIG_FILE` | Path to an alternative config file |
A token-based session needs only two variables:
export DATABRICKS_HOST="https://dev-workspace.cloud.databricks.com"
export DATABRICKS_TOKEN="<personal-access-token>"
databricks current-user meA service principal session uses the client ID and secret instead:
export DATABRICKS_HOST="https://prod-workspace.cloud.databricks.com"
export DATABRICKS_CLIENT_ID="<application-id>"
export DATABRICKS_CLIENT_SECRET="<oauth-secret>"
databricks current-user meThe same variables are what the REST examples in the next chapters read. The curl and requests code uses DATABRICKS_HOST and DATABRICKS_TOKEN directly, so exporting them once covers both the CLI and your own scripts.
Precedence: which setting wins
When the same setting appears in several places, the tool has to pick one. The Databricks tools follow a fixed order. For the CLI, the order for most settings is:
- Command-line flags, such as
--profileor--host. - Environment variables.
- The selected profile in
~/.databrickscfg.
Bundles add one more source: the workspace section of databricks.yml and the -t target. These are covered in the bundle chapters.
The SDK uses a similar idea, called a credential chain. It looks at settings passed in code first, then environment variables, then the profile file, and tries supported authentication methods until one works.
The diagram is simplified. In practice, settings are resolved key by key, which leads to the most common profile bug.
Common errors and gotchas
Mixed credentials from different sources
Suppose your DEFAULT profile points at the dev workspace and you export DATABRICKS_HOST for production but forget the token. The tool may combine the production host from the environment with credentials from the profile. The result is usually an authentication error, and in the worst case a request against a workspace you did not intend.
Two habits prevent this:
- Set
DATABRICKS_HOSTand its credentials together, or use onlyDATABRICKS_CONFIG_PROFILE. - Run
databricks current-user meafter changing anything.
Conflicting authentication methods
If a profile defines both a token and a client_id/client_secret pair, or the environment supplies a second method alongside the profile, the SDK and CLI can report an error about more than one authentication method being configured. Remove the extra values, or set auth_type to say which method you mean. For example, auth_type = pat selects the personal access token.
An exported variable you forgot about
A DATABRICKS_TOKEN left in your shell profile (.bashrc or .zshrc) overrides what the config file says. If a command acts as the wrong user, check what is exported:
env | grep '^DATABRICKS_' | sed 's/\(TOKEN=\|SECRET=\).*/\1***/'The sed part hides secret values so the output is safe to paste into a chat or ticket.
Expired OAuth login
A profile created with databricks auth login fails once its refresh token expires, often with a message that the token is invalid or the login has expired. Run databricks auth login --profile <name> again. Non-interactive jobs should use a service principal rather than a user login for this reason.
Wrong host format
Use the full workspace URL with https:// and no path, such as https://dev-workspace.cloud.databricks.com. A trailing path like /browse or a missing scheme causes confusing failures. The Python example later in this tutorial strips a trailing slash with rstrip("/"), but not every tool does.
Using profiles in the SDK and bundles
The Python SDK accepts a profile name directly:
from databricks.sdk import WorkspaceClient
dev = WorkspaceClient(profile="DEFAULT")
staging = WorkspaceClient(profile="staging")
print(dev.current_user.me().user_name)
print(staging.current_user.me().user_name)Creating two clients in one script is a clean way to compare or copy settings between workspaces. With no arguments, WorkspaceClient() follows the credential chain described above.
For bundles, you pass the profile with the same flag:
databricks bundle validate -t dev --profile DEFAULTA common pattern is to pair each bundle target with a profile of the same name, so -t staging and --profile staging always go together.
Choosing between profiles and environment variables
| Situation | Better choice | Reason |
|---|---|---|
| Daily work on your laptop | Profiles with OAuth login | No secret on disk, easy switching with `--profile` |
| CI/CD runner | Environment variables from the secret store | No config file to manage, secrets stay in the CI system |
| Several workspaces in one script | Profiles | Each client or command names its target explicitly |
| One-off command against another workspace | `--profile` or `--host` flag | Does not change your shell state |
Exercise
Your DEFAULT profile points at the dev workspace. In a CI job you set DATABRICKS_HOST to the production URL and DATABRICKS_CLIENT_ID and DATABRICKS_CLIENT_SECRET for a service principal. You do not set a profile. A teammate worries the job will use the dev profile. What is the likely behavior, which command would you add to the job to confirm it, and what extra risk exists if the runner also has a ~/.databrickscfg file?
Show answer
Environment variables rank above the profile file, so the job uses the production host and the service principal credentials. Add databricks current-user me as the first step to print the identity in use. If the runner has a ~/.databrickscfg, settings that the environment does not supply (such as a leftover token or auth_type in DEFAULT) can be merged in, which can cause conflicting authentication errors or an unintended identity. Remove the file from the runner, or set DATABRICKS_CONFIG_FILE to a non-existent path and keep all settings in environment variables.
Exercise
Write the command that lists jobs in a workspace using a profile named staging without changing any environment variable, and the command that makes staging the profile for the rest of the current shell session.
Show answer
databricks jobs list --profile staging uses the profile for one command only. export DATABRICKS_CONFIG_PROFILE=staging makes it the default for every later command in that shell.