Non-interactive login in Azure CLI (2.0)

More than year ago I described here how to perform non-interactive login to Azure CLI - the "old", xplat one. Just for future linking purposes I will describe the same how-to for Azure CLI 2.0.

Create an AAD app and a Service Principal for RBAC - you need to be logged in as a user with Owner permissions. (example for Resource Group)

az ad sp create-for-rbac -n "https://<yourapp-address>" --role contributor --scopes /subscriptions/<subscription-id>/resourceGroups/<rg-name>

Just replace , and with proper values. This command will output:

{
"appId": "<app-id>",
"displayName": "<display-name>",
"name": "https://<yourapp-address>",
"password": "<pass-key>",
"tenant": "<tenant>"
}

Login to az using Service Principal

az login --service-principal -u <app-id> -p <pass-key> --tenant <tenant>