Home > Mobile >  How do I authenticate a bash script running azcli commands
How do I authenticate a bash script running azcli commands

Time:11-19

I have a bash script vault.sh

az login
Source_Kv_Name="myKeyVault2020"
SECRETS =($(az keyvault secret list --vault-name $Source_Kv_Name --query "[].id" -o tsv))

If I run it as bash vault.sh it fails to connect to vault (authenticate)

If I run the same commands from terminal, not the script, it works fine. Why is happening, and how do I authenticate bash script to run the same?

CodePudding user response:

What is the error? Can you share the output? I can say that for a bash script usually you need to "hard code users password" on the script, or use SPN authentication. If your script is running from Azure Automation, you can use the Identity Managment on the Azure Automation and give access to the automation account to the component and use that access. Example:

$azContext = (Connect-AzAccount -Identity).context

CodePudding user response:

I tried to reproduce the same in my environment and got the result successfully.

In my bash I login with az login like below:

enter image description here And copy the Https://microsoft.com/devicelogin in browser and enter the code -> continue and close the tab like below:

enter image description here

Now, when a create a file vi vault.sh run same script

az login
Source_Kv_Name="khankeyvault "
az keyvault secret list --vault-name $Source_Kv_Name --query "[].id" -o tsv

enter image description here

When I run bash vault.sh I got authenticate login as same and got the result successfully like below:

enter image description here

  • Related