1

Trying to save an output of azure advisor recommendation on to a variable so that i can pass on to next task.

However no matter what syntax I try, and believe me i have tried all possible combinations, the variable doesn't get saved. Interestingly, these work in cloud shell (bash) for E.g.

rgName="$(az group list --query "[?tags.Test=='yes'].name" --output tsv)" 
az group show -n $rgName

This works just fine in cloud shell. But not in DevOps azurecli task. I also referred to multiple examples given in stack overflow itself, but none of them work. Using task version 1.*

rgName' is not recognised as an internal or external command,

Can someone give an working example for DevOps Task?

Note: BTW the whole reason for me using cli is because cant find Advisor RM module and the Az module wont load correctly in task version 4.*

Deviyani Swami
  • 749
  • 8
  • 17
Sumesh
  • 123
  • 2
  • 13
  • 1
    Do you need save a variable to the next tasks? did you try `echo "##vso[task.setvariable variable=rgName']value"` ? – Shayki Abramczyk May 23 '19 at 07:49
  • no, that is what i am looging for. I simply couldnt save the results to 'rgName=' in the command rgName="$(az group list --query "[?tags.Test=='yes'].name" --output tsv)" – Sumesh Jul 12 '19 at 05:02

2 Answers2

0

As Shayki mentioned above, task.setvariable can help with setting a variable from a script. The same has been detailed in this document. In a nutshell, you would have to do this:

rgName=$(az group list --query "[?tags.Test=='yes'].name" -o tsv | tr '\n' ' ')
echo "##vso[task.setvariable variable=RESULT]$rgName"

The task.setvariable is a logging command and does not update the environment variables, but it does make the new variable available to downstream steps within the same job. Notice that the results are separated by CRLF and not spaces, and hence the trimming tr '\n' ' '. Now, in the subsequent tasks where you need the variable, you may use it in this way:

echo "Result: $(RESULT)"

Refer to this blog to get a detailed walkthrough. Hope this helps!

Bhargavi Annadevara
  • 4,923
  • 2
  • 13
  • 30
  • Thanks, I am aware of task.setvariable.My issue was with getting the results of the command into 'rgName' variable. Common sense says that rgName='whatever command' would save the results into 'rgName' but for some reason it wasnt working in this task. If the first step itself doesnt work, then i cant use echo "##vso[task.setvariable variable=RESULT]$rgName" because, based on what i was facing $rgName would always be null as the results arent getting saved there. – Sumesh Jul 12 '19 at 05:03
  • Are you still blocked on this? – Bhargavi Annadevara Jul 12 '19 at 05:07
  • I have moved past the issue using other means, however the issue still EXISTS when i tested today. no matter what syntax i try, i cant get this to work inside the CLI task. same syntax works in cloushell though. – Sumesh Jul 12 '19 at 11:57
0

What fixed this for me was to use a Linux agent job and not a Windows agent then add the Azure CLI task to run on the Linux agent, specifically the ubuntu-16.04.