Sub topic 4
Profiles and environment variables
This chapter assumes you have installed the Databricks CLI and logged in once with databricks auth login. It also assumes you know how to set environment variables in your shell. Here you will learn how the CLI stores credentials for several workspaces and how it decides which ones to use.
What a profile is
A profile is a named section in the ~/.databrickscfg file. Each section holds the connection details for one workspace. The login command from the previous chapter creates one for you.
[DEFAULT]
host = https://dev-workspace.cloud.databricks.com
auth_type = databricks-cli
[prod]
host = https://prod-workspace.cloud.databricks.com
auth_type = databricks-cliThe DEFAULT profile is used when you do not name one. A profile saves the host and the authentication type. For OAuth logins it does not store a password. The token is cached separately and refreshed as needed.
Creating and choosing profiles
Use --profile at login to save a login under a name of your choice.
databricks auth login --host https://<workspace-host> --profile prod
databricks auth profiles
databricks current-user me --profile proddatabricks auth profiles lists every profile and shows whether its credentials are valid. Pass --profile (or -p) to any command to pick one. Without it, the CLI uses DEFAULT.
Environment variables
The CLI and SDKs also read environment variables. These are the ones you will use most.
| Variable | Purpose |
|---|---|
| `DATABRICKS_HOST` | Workspace URL |
| `DATABRICKS_TOKEN` | Personal access token |
| `DATABRICKS_CONFIG_PROFILE` | Name of the profile to use |
| `DATABRICKS_CONFIG_FILE` | Path to a config file other than `~/.databrickscfg` |
Environment variables suit CI systems and containers, where no interactive login is possible and no config file exists.
export DATABRICKS_HOST=https://<workspace-host>
export DATABRICKS_TOKEN=<token>
databricks current-user mePrecedence
The CLI resolves each setting from several sources. In general, the order is:
- Command-line flags, such as
--profileor--host. - Environment variables.
- The selected profile in
~/.databrickscfg.
The diagram below shows the lookup.
Treat this order as a guide and confirm it for your CLI version. Mixing sources can produce surprising results.
Common errors
- Wrong workspace. A stale
DATABRICKS_HOSTin your shell can point a command at a different workspace than the profile you named. Runenv | grep DATABRICKSwhen results look wrong. - Conflicting credentials. If
DATABRICKS_TOKENis set and the profile uses OAuth, the CLI may report that more than one authentication method is configured. Unset the variable you do not want. - Expired login. OAuth sessions expire. Run
databricks auth login --profile <name>again. - Profile not found. Names are case-sensitive. Check them with
databricks auth profiles.
Exercise
Your shell has DATABRICKS_HOST set to the dev workspace. You run a command with --profile prod and it still fails with an authentication conflict. What is the likely cause, and what is one way to fix it?
Show answer
The environment variable and the prod profile both supply a host, so the CLI sees conflicting configuration. Unset the variable with unset DATABRICKS_HOST and run the command again, or remove the profile flag and rely only on the environment.