{"library":"azure-cli-core","code":"import os\nfrom azure.cli.core import get_default_cli\n\n# Initialize the Azure CLI core\naz_cli = get_default_cli()\n\n# Example 1: Run a simple command (e.g., list resource groups)\n# Ensure you are logged in via `az login` or have AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID set\nprint(\"\\n--- Listing Azure resource groups ---\")\ntry:\n    exit_code_list = az_cli.run(['group', 'list', '--output', 'json'])\n    print(f\"'az group list' command exited with code: {exit_code_list}\")\nexcept Exception as e:\n    print(f\"Error running 'az group list': {e}\")\n\n# Example 2: Create a dummy resource group (requires authentication and permissions)\nresource_group_name = os.environ.get('AZURE_TEST_RG_NAME', 'my-test-rg-cli-core')\nlocation = os.environ.get('AZURE_TEST_LOCATION', 'eastus')\n\nprint(f\"\\n--- Attempting to create resource group: {resource_group_name} in {location} ---\")\ntry:\n    exit_code_create = az_cli.run(['group', 'create', '--name', resource_group_name, '--location', location, '--output', 'json'])\n    print(f\"'az group create' command exited with code: {exit_code_create}\")\nexcept Exception as e:\n    print(f\"Error running 'az group create': {e}\")\n\n# Example 3: Clean up the dummy resource group (if created successfully)\nprint(f\"\\n--- Attempting to delete resource group: {resource_group_name} ---\")\ntry:\n    # Use --yes for non-interactive deletion\n    exit_code_delete = az_cli.run(['group', 'delete', '--name', resource_group_name, '--yes', '--output', 'none'])\n    print(f\"'az group delete' command exited with code: {exit_code_delete}\")\nexcept Exception as e:\n    print(f\"Error running 'az group delete': {e}\")\n","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}