2

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

Jahnavi
  • 3,076
  • 1
  • 3
  • 10
js352
  • 364
  • 2
  • 9
  • https://stackoverflow.com/questions/51546073/how-to-run-azure-cli-commands-using-python – Sajeetharan Jul 19 '20 at 17:21
  • 1
    @js352you can use service principal to login Azure. – Jim Xu Jul 20 '20 at 08:13
  • My question goes more on this way: If I have different functions to retrieve information, how do I put it together with the authentication so it works for all of them. So far I have a line with the auth: `os.system(["az", "login", "--service-principal", "--username", username, "--password", password, "--tenant", tenant])` and then I call the functions. But these ones return none. – js352 Jul 20 '20 at 16:55

1 Answers1

4

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

Jim Xu
  • 21,610
  • 2
  • 19
  • 39