Azure CLI Core

2.85.0 · active · verified Wed Apr 08

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Azure CLI core and execute Azure CLI commands programmatically within a Python script using `get_default_cli().run()`. It includes examples for listing, creating, and deleting a resource group. Ensure you are authenticated to Azure (e.g., via `az login` in your terminal or by setting relevant environment variables) for commands requiring credentials to succeed.

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}")

view raw JSON →