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.
pip install az.cli==0.5Verified import paths — ran on the pinned version, not inferred.
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.
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.
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`.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.
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.