Azure CLI Core
The `azure-cli-core` library provides the core runtime and fundamental services for the Microsoft Azure Command-Line Interface (Azure CLI). It is a key internal component that enables the Azure CLI to function, providing capabilities such as command dispatch, argument parsing, and output formatting. The current version is 2.85.0, and the Azure CLI project typically follows a monthly release cadence, including updates to this core module.
Warnings
- gotcha `azure-cli-core` is primarily an internal component. For most users, installing the full `azure-cli` package (`pip install azure-cli`) or using one of the standalone installers (MSI, Deb, RPM, Homebrew) is the recommended way to get a functional Azure CLI. Installing `azure-cli-core` alone will not provide the full CLI experience and may lack necessary command modules.
- breaking The minimum required Python version for `azure-cli-core` (and thus `azure-cli`) can change with major releases. As of version 2.85.0, Python >=3.10.0 is required. Older versions may support or require different Python versions. Ensure your environment meets the `requires_python` specification.
- gotcha When executing CLI commands programmatically using `az_cli.run()`, the output format and command syntax are subject to change between Azure CLI versions. Scripts relying on specific JSON output structures or command parameters may break unexpectedly with CLI updates.
- gotcha Programmatic execution via `get_default_cli().run()` relies on the same authentication context as the standalone Azure CLI. If your script is not running in an authenticated environment (e.g., `az login` not performed, or relevant environment variables not set for service principal login), commands requiring authentication will fail.
Install
-
pip install azure-cli-core -
pip install azure-cli
Imports
- get_default_cli
from azure.cli.core import get_default_cli
Quickstart
import os
from azure.cli.core import get_default_cli
# Initialize the Azure CLI core
az_cli = get_default_cli()
# Example 1: Run a simple command (e.g., list resource groups)
# Ensure you are logged in via `az login` or have AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID set
print("\n--- Listing Azure resource groups ---")
try:
exit_code_list = az_cli.run(['group', 'list', '--output', 'json'])
print(f"'az group list' command exited with code: {exit_code_list}")
except Exception as e:
print(f"Error running 'az group list': {e}")
# Example 2: Create a dummy resource group (requires authentication and permissions)
resource_group_name = os.environ.get('AZURE_TEST_RG_NAME', 'my-test-rg-cli-core')
location = os.environ.get('AZURE_TEST_LOCATION', 'eastus')
print(f"\n--- Attempting to create resource group: {resource_group_name} in {location} ---")
try:
exit_code_create = az_cli.run(['group', 'create', '--name', resource_group_name, '--location', location, '--output', 'json'])
print(f"'az group create' command exited with code: {exit_code_create}")
except Exception as e:
print(f"Error running 'az group create': {e}")
# Example 3: Clean up the dummy resource group (if created successfully)
print(f"\n--- Attempting to delete resource group: {resource_group_name} ---")
try:
# Use --yes for non-interactive deletion
exit_code_delete = az_cli.run(['group', 'delete', '--name', resource_group_name, '--yes', '--output', 'none'])
print(f"'az group delete' command exited with code: {exit_code_delete}")
except Exception as e:
print(f"Error running 'az group delete': {e}")