简体   繁体   English

Azure DevOps Yaml:从变量 Azure KeyVault 任务中获取秘密变量

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

I'm trying to obtain a secret out of my KeyVault.我正试图从我的 KeyVault 中获取一个秘密。 The variable name is secretVar.变量名是secretVar。

Obtaining the secret like this: $(secretVar) works fine however I would like to retrieve it from a variable like this:像这样获取秘密: $(secretVar) 工作正常,但是我想从这样的变量中检索它:

在此处输入图像描述

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.所以我要提取的秘密名称在 bash 变量中。 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:对于这个问题,我已经简化了问题,但在我的实际用例中,我有一个 bash for 循环,它遍历秘密名称,在 for 循环中,我想从具有相应秘密名称的 KeyVault 中提取适当的值,如下所示:

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您正在使用文字值secretVar创建环境变量

Then you try to execute the value of the variable $variable with $($variable) .然后您尝试使用$($variable)执行变量$variable的值。 So it tries to run the command secretVar , which obviously doesn't exist, and you get an error message.因此它尝试运行命令secretVar ,该命令显然不存在,并且您会收到一条错误消息。

The syntax you're looking for is您正在寻找的语法是

variable=$(secretVar)

just like you used in the first echo command in the script.就像您在脚本中的第一个echo命令中使用的一样。

If you don't want to run the variable value as a command, the syntax would be $variable , not $($variable)如果您不想将变量值作为命令运行,则语法为$variable ,而不是$($variable)

$variable is the syntax for a Bash environment variable. $variable是 Bash 环境变量的语法。

$(variable) is the syntax for referencing Azure DevOps variables. $(variable)是引用 Azure DevOps 变量的语法。

First of all, the script keyword is a shortcut for the command-line task.首先, script关键字是命令行任务的快捷方式。 The task runs a script using cmd.exe on Windows and Bash on other platforms.该任务在 Windows 和 Bash 上使用 cmd.exe 运行脚本。 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.如果要在脚本中设置变量,可以使用 task.setvariable 日志命令。 For example:例如:

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

You can find more detailed information in this document .您可以在本文档中找到更详细的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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