1

I am trying to use the "az pipelines variable-group variable create ..." command to create a variable which references a different variable. e.g.

az pipelines variable-group variable create --project MyTestProject -- 
group-id 15 --name ‘ApplicationName’ --value 'TestApp-$(env)' 

where the variable “env” is defined in a variable-group within the same project library.

When I run the command above it gives the error:

“Failed to load python executable” “exit /b 1”. 

Despite an error being generated the variable is created; in the example above the variable 'ApplicationName' has the value 'TestApp-$(env', the trailing bracket character, ")", is missing and seems to be causing the problem.

The dollar sign, "$", and/or the opening bracket character "(" don't generate any error when used without the closing bracket ")".

I have tried escaping the closing bracket character with backslashes "\" and caret "`" characters but couldn't find any combination that would create the desired variable value, "TestApp-$(env)".

Could someone tell me how to escape the closing bracket so the variable is correctly created within the variable group.

I am running the following versions of az:

azure-cli                         2.0.73
command-modules-nspkg               2.0.3
core                              2.0.73
nspkg                              3.0.4
telemetry                          1.0.3
Extensions:
azure-devops                      0.12.0

Python (Windows) 3.6.6 

Many Thanks,

Gary

Gary D
  • 51
  • 1
  • 4

3 Answers3

4

I have finally managed to figure out how to escape a variable whose name itself contains a different variable name. By calling the az cli command and wrapping the variable value in double quotes and a single quote, the variable is correctly created in DevOps:

pipelines variable-group variable create --project MyTestProject -- group-id 15 --name ApplicationName' --value '"TestApp-$(env)"' 

DevOps-LibraryVariable-screenshot

Nick Westgate
  • 3,088
  • 2
  • 34
  • 41
Gary D
  • 51
  • 1
  • 4
  • This works if you are passing the value directly in that command, doesn't work if the the value is in a variable and you are passing the variable. But good find, thanks! – Abhirup Guha Oct 21 '20 at 05:35
1

As a further expansion on this topic I had a need to pass the value as a variable. My source was in a key value pair. In this case I used the following.

$key = $var.Key
$value = '"{0}"' -f $var.Value
    
az pipelines variable-group variable update --group-id $groupId --org $org --project $project `
--name $key --value $value
LepardUK
  • 1,330
  • 1
  • 11
  • 17
0

It depends on your OS and tools.

For example, in Windows OS, you can get environment variable with %variable_name%. So, the following would be right:

az pipelines variable-group variable create --project keyvault --group-id 1 --name "ApplicationName" --value "TestApp-%java_home%"

enter image description here

However, in PowerShell, you can get environment with "$env:variable_name". So, the following would be right:

az pipelines variable-group variable create --project keyvault --group-id 1 --name "ApplicationName2" --value "TestApp-$($env:java_home)"

enter image description here


Update:

So, in Azure Pipeline, you can use group variables as following:


pool: 
  name: Hosted VS2017
  demands:
  -  msbuild
  -  visualstudio
  -  vstest

variables:
  - group: vargroup

steps:
- task: AzureCLI@1
  inputs:
    azureSubscription: 'CSP Azure (e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68)'
    scriptLocation: 'inlineScript'
    inlineScript: |
      echo the variable var1:%var1%

I have a variable group:

enter image description here

And you can see that: echo the variable var1:%var1% will be echo the variable var1:value1

enter image description here

Jack Jia
  • 5,268
  • 1
  • 12
  • 14
  • Hi Jack, thanks for your response. My problem isn't that resolving a variable value and having it stored in a variable group, my problem is that the value I want to have in my variable group should actually contain a variable within the value. So using your example, instead of "value": "TestApp-C:\\Program Files\\Java\\jdk1.8.0_192" I want "value": "TestApp-$(java_home)". Where the variable $(java_home) is defined within a variable group and not taken for an OS environment. Apologies if my description was misleading; thanks again for taking the time to try and answer the question – Gary D Sep 19 '19 at 14:25
  • Hi Jack, thanks again for the reply. My problem isn’t evaluating an environment variable at runtime, but rather defining a variable whose name contains a variable within the name. I have finally managed to figure out a solution to my problem, in the end it was simple. I’ll document my solution below. Cheers, Gary – Gary D Sep 25 '19 at 12:56