Home > Enterprise >  AzureKeyVault@2 Task & retrieve key vault value retrieval by variable?
AzureKeyVault@2 Task & retrieve key vault value retrieval by variable?

Time:10-06

So I have a variable in a variables.yaml file called keyVaultSecretToRetrieve. It can vary based on environment. Previously this pipeline was coded for single environment. So lets say the keyVaultSecretToRetrieve is "secret1". If in the task I put SecretsFilter: "secret1" and run the task and try to reference $(secret1) in the following task everything works great. But my problem is that if I put if like the example below as SecretsFilter $(keyVaultSecretToRetrieve). How can I then retrieve the value? Its almost like I would want to do something like if it where possible $($(keyVaultSecretToRetrieve)). I know my other option is to just run the command in a script using azure CLI which I tried but the build server says it does not have azure cli installed and I'd rather not mess with it and just wrap this up quickly. Not sure if there is some way to achieve what I want? Its a windows build agent and the steps after this are mostly powershell. I wish the task just returns a data structure vs. dynamically named variables.... This is throwing me off it there is some trick I can do to make it work that I'm not aware of vs. just being forced to go the azure cli route or have the variable name coded in the variables name vs. as another variables.....

         - task: AzureKeyVault@2
            displayName: Retrieve from keyvault
            inputs:
              azureSubscription: $(serviceConnection)
              KeyVaultName: $(keyVaultName)
              SecretsFilter: $(keyVaultSecretToRetrieve)
              RunAsPreJob: false

CodePudding user response:

Based on your requirement, you need to use nested variable: $($(keyVaultSecretToRetrieve)). There is no built-in feature can support this requirement.

To achieve your requirement, you can use the Variable Set task from extension: Variable Toolbox.

For example:

steps
- task: VariableSetTask@2
  inputs:
    variableName: NewVariable
    Value: '$($(keyVaultSecretToRetrieve))'

It will create a new variable based on the nested variable:$($(keyVaultSecretToRetrieve)). Then you can use the $(NewVariable) in the next tasks.

  • Related