简体   繁体   中英

Azure DevOps Yaml: Gaining secret variable out of Azure KeyVault Task from Variable

I'm trying to obtain a secret out of my KeyVault. The variable name is secretVar.

Obtaining the secret like this: $(secretVar) works fine however I would like to retrieve it from a variable like this:

在此处输入图像描述

I keep getting command not found and I've no idea why this shouldn't be working.

So the name of the secret I want to extract is inside a bash variable. For this question I've simplified the problem but in my real use case I have a bash for loop which loops through secret names and inside the for loop I want to extract the appropriate value from the KeyVault with the corresponding secret name like this:

for secretname in secrets; do
  echo $($secretname) # This should contain the value of the secret but gives command not found
done

If anyone has an idea what could be happening, any help is very appreciated.

Thanks in Advance!

Look at the syntax you're using.

variable=secretVar

You are creating an environment variable with the literal value secretVar

Then you try to execute the value of the variable $variable with $($variable) . So it tries to run the command secretVar , which obviously doesn't exist, and you get an error message.

The syntax you're looking for is

variable=$(secretVar)

just like you used in the first echo command in the script.

If you don't want to run the variable value as a command, the syntax would be $variable , not $($variable)

$variable is the syntax for a Bash environment variable.

$(variable) is the syntax for referencing Azure DevOps variables.

First of all, the script keyword is a shortcut for the command-line task. The task runs a script using cmd.exe on Windows and Bash on other platforms. You need to pay attention to the agent you are using.

If you want to set variables in scripts, you can use task.setvariable logging command. For example:

- script: |
    echo $(secretvar)
    echo "##vso[task.setvariable variable=variable]$(secretvar)"
- script: |
    echo $(variable)

You can find more detailed information in this document .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM