如何在没有手动配置的情况下使用 databricks-cli

How do I use databricks-cli without manual configuration

我想使用databricks cli:

databricks clusters list

但这需要手动步骤,需要与用户进行交互:

databricks configure --token

有没有一种无需手动干预即可使用 databricks cli 的方法,以便您可以 运行 将其作为 ci/cd 管道的一部分?

以下bash脚本,自动配置databricks cli:

echo "configuring databrick-cli authentication"

declare DATABRICKS_URL="https://westeurope.azuredatabricks.net"
declare DATABRICKS_ACCESS_TOKEN="authentication_token_generated_from_databricks_ux"

declare dbconfig=$(<~/.databrickscfg)
if [[ $dbconfig = *"host = "* && $dbconfig = *"token = "* ]]; then
  echo "file [~/.databrickscfg] is already configured"
else
  if [[ -z "$DATABRICKS_URL" || -z "$DATABRICKS_ACCESS_TOKEN" ]]; then
    echo "file [~/.databrickscfg] is not configured, but [DATABRICKS_URL],[DATABRICKS_ACCESS_TOKEN] env vars are not set"
  else
    echo "populating [~/.databrickscfg]"
    > ~/.databrickscfg
    echo "[DEFAULT]" >> ~/.databrickscfg
    echo "host = $DATABRICKS_URL" >> ~/.databrickscfg
    echo "token = $DATABRICKS_ACCESS_TOKEN" >> ~/.databrickscfg
    echo "" >> ~/.databrickscfg
  fi
fi

您可以只导出变量 DATABRICKS_HOST 和 DATABRICKS_TOKEN。使用这些变量,您不需要配置文件。

正如@usingnamespace 和官方文档中提到的:

CLI 0.8.0 and above supports environment variables, an environment variable setting takes precedence over the setting in the configuration file.

DATABRICKS_HOST
DATABRICKS_USERNAME
DATABRICKS_PASSWORD
DATABRICKS_TOKEN

有了它,您不仅 不会 暴露明文文件中的敏感数据 (~/.databrickscfg),您还赢了不需要向您的脚本添加更多代码。

这是脚本的 PowerShell 版本。

write-host Configure databricks access
$Env:DATABRICKS_CONFIG_FILE = "$(System.DefaultWorkingDirectory)/.databrickscfg"
Set-Location $(System.DefaultWorkingDirectory)
Set-Content .databrickscfg "[DEFAULT]"
Add-Content .databrickscfg "host = https://westeurope.azuredatabricks.net/"
Add-Content .databrickscfg "token = $(db-token)"