Q:

Azure pipeline output variables task.setvariable is not assigning any value to variable

I am trying to execute terraform script to create ec2 and using output variables concept I want to get the ec2 public ip and assign it to a variable and pass that to another task. But I see that output variables using bash or script is not working in azure pipeline yml. I am using ubuntu agent.

script: COMMAND=$(cat terraform.tfstate | grep 'public_ip"' | cut -d":" -f2 | cut -d'"' -f2) && echo "##vso[task.setvariable variable=ec2ip;]$COMMAND" && echo $ec2ip
But I see no value is printing. Not sure whether the task.setvarible is working or not.
Need some help to fix this.

A:

You cannot echo the new variable's value in the same script task. The new variable set by statement task.setvariable is only available in the subsequent tasks.
You can add a subsequent script task to echo variable ec2ip. And you retrieve its value by wrapping it in $() (ie.$(ec2ip)). Please check document Define vairables for more information.
- script: COMMAND=$(cat terraform.tfstate | grep 'public_ip"' | cut -d":" -f2 | cut -d'"' -f2) && echo "##vso[task.setvariable variable=ec2ip;]$COMMAND" 

- script: echo "$(ec2ip)"

