Search code examples
pythonazureazure-cli

Authenticating Azure CLI with Python SDK


I am writing some functions to extract data from Azure. I am using the Python subprocess library with the Azure CLI commands as they are easier and better documented thank the Python SDK. My question comes whether it is possible to combine the Azure CLI commands with the Python SDK to make the authentication as the CLI uses interactive login and don't have many choices.

The goal of this is to incorporate those functions into a bigger script that authenticates and gets all the information we need.

Any ideas or ways of doing this would be appreciate it.

Thank you


Solution

  • According to my test, if you want to call Azure CLI command in python application, we can use the package azure-cli.

    For example

    from azure.cli.core import get_default_cli
    
    az_cli = get_default_cli()
    
    az_cli.invoke(['login', '--service-principal', '-u', '<appId>', '-p', 'password','--tenant','teanat id'])
    
    az_cli.invoke(['group','show', '-n', 'jimtest'])
    

    enter image description here