{"id":5122,"library":"az.cli","title":"Azure CLI Python Interface","description":"az.cli provides a simple Python interface to execute Azure CLI commands directly from Python scripts. It leverages the subprocess module to call the `az` executable installed on the system, passing commands as strings and returning exit codes, parsed JSON results, and raw logs. The current version is 0.5, with releases occurring infrequently as new features or critical bug fixes are implemented.","status":"active","version":"0.5","language":"en","source_language":"en","source_url":"https://github.com/MarkWarneke/Az.Cli","tags":["azure","cli","automation","wrapper","subprocess"],"install":[{"cmd":"pip install az.cli==0.5","lang":"bash","label":"Install specific version"}],"dependencies":[{"reason":"This library acts as a wrapper around the Azure CLI executable. It must be installed, configured, and authenticated separately on the host system where the Python script runs.","package":"Azure CLI (external executable)","optional":false}],"imports":[{"symbol":"az","correct":"from az.cli import az"}],"quickstart":{"code":"from az.cli import az\n\n# Execute an Azure CLI command\n# For example, to list resource groups. Make sure you are authenticated with Azure CLI (e.g., 'az login').\nexit_code, result_dict, logs = az(\"group list\")\n\nif exit_code == 0:\n    print(\"Azure CLI command executed successfully!\")\n    print(\"Parsed Result (if JSON output):\", result_dict)\n    if result_dict and isinstance(result_dict, list) and len(result_dict) > 0:\n        print(f\"First resource group name: {result_dict[0].get('name', 'N/A')}\")\nelse:\n    print(f\"Azure CLI command failed with exit code: {exit_code}\")\n    print(\"Error logs:\", logs)\n\n# Example of a command that might not return JSON or returns raw string output\n# exit_code, result_dict, logs = az(\"account show --output tsv\")\n# if exit_code == 0:\n#     print(\"TSV Output (result_dict will be empty for non-JSON):\")\n#     print(logs) # Raw output will be in 'logs' string\n","lang":"python","description":"This quickstart demonstrates how to import and use the `az` function to execute an Azure CLI command. It's critical to check the `exit_code` for command success and to understand that `result_dict` only contains parsed JSON output. For other formats or errors, the raw output is in `logs`. Ensure your Azure CLI is installed and you are logged in for commands like `group list` to work."},"warnings":[{"fix":"Install the Azure CLI via its official documentation (e.g., apt, yum, brew, PowerShell, MSI installer) and ensure it's in your system's PATH. Then, run `az login` to authenticate.","message":"The `az.cli` Python library is merely a wrapper. It does NOT bundle the Azure CLI executable. You MUST have the Azure CLI installed and authenticated on the host system where your Python script runs for `az.cli` to function correctly.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always unpack the return value (e.g., `exit_code, result_dict, logs = az(\"command\")`) and explicitly check `if exit_code != 0:` to handle command failures and retrieve error messages from `logs`.","message":"Error handling for Azure CLI commands through `az.cli` relies on checking the `exit_code` and `logs` returned by the `az()` function. Failures in the underlying CLI command do not raise Python exceptions directly but are indicated by a non-zero `exit_code` and detailed messages in `logs`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To utilize `result_dict`, ensure your Azure CLI commands explicitly request JSON output (e.g., `az account show --output json`). For other formats, process the `logs` string directly.","message":"The `result_dict` returned by `az()` is populated only when the underlying Azure CLI command successfully outputs valid JSON. For commands that produce other output formats (e.g., `--output tsv`, `--output table`) or on command failure, `result_dict` will be empty or `None`. In such cases, the raw output will be available in the `logs` string.","severity":"gotcha","affected_versions":"All versions"},{"fix":"While `az.cli` itself has minimal Python dependencies, users should ensure their environment has any necessary basic packages (like a JSON parser, though Python's `json` module is standard) if issues arise with `result_dict` processing.","message":"Starting with version 0.4, the `requirements.txt` file was removed to promote a 'loosely coupled' approach. This means the library no longer declares or enforces its direct Python package dependencies (e.g., for JSON parsing).","severity":"deprecated","affected_versions":">=0.4"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}