microsoft / azure-pipelines-yaml

Azure Pipelines YAML examples, templates, and community interaction
MIT License
1.21k stars 934 forks source link

how to use "Each" Template Expression for a json array text in variable #539

Closed anilsamuel closed 3 years ago

anilsamuel commented 3 years ago

I have a json array in ip_addrs task variable as below. Is it possible to iterate through this array using Each or similar ?

- task: AmazonWebServices.aws-vsts-tools.AWSShellScript.AWSShellScript@1
      name: getIPAddrs
      displayName: 'Obtain EC2 IP Addresses'
      inputs:
        awsCredentials: 'test'
        regionName: 'ap-southeast-2'
        scriptType: 'inline'
        inlineScript: |
          sudo apt-get -y install jq
          ip_addrs=$(aws ec2 describe-instances --query 'Reservations[*].Instances[*].[LaunchTime,InstanceId,ImageId,PrivateIpAddress,Tags[?Key==`Name`] | [0].Value][] | sort_by(@, &[4])' --output json --filters Name=instance-state-name,Values=running | jq -c '.[] | select(.[4]|startswith("PHREDC"))' | tr -s '\r\n' ',')
          echo "##vso[task.setvariable variable=ip_addrs;isOutput=true]"["${ip_addrs::-1}"]""
anatolybolshakov commented 3 years ago

Hi @anilsamuel you can't use runtime value for each command - please see docs for some examples.

anilsamuel commented 3 years ago

ok. Is there any other I can loop through the values in ip_addrs and create a job ?

xinfli commented 3 years ago

No, I think it's impossible; I have spend several hours on same problem and finally realized this.

  1. You can loop parameter with array value, but the array value have to be hard coded in yaml pipeline;
  2. You can not pass an array dynamic generated to parameter since variable does not support array value;

Why it's designed as so?

anatolybolshakov commented 3 years ago

@xinfli @anilsamuel this repository is mostly for yaml templates at the moment - please feel free to open ticket on https://developercommunity.visualstudio.com/search?space=21 for further questions. Let me close this at the moment since it's not related to templates which repository contains.

anilsamuel commented 3 years ago

No, I think it's impossible; I have spend several hours on same problem and finally realized this.

  1. You can loop parameter with array value, but the array value have to be hard coded in yaml pipeline;
  2. You can not pass an array dynamic generated to parameter since variable does not support array value;

Why it's designed as so?

The intent is to obtain the ip address of the ec2 instances and to test the custom sftp service on it. The ip address is dynamic hence cannot be known in advcance; hence the aws cli query to dynamically obtain the ip address.

Could you think of a better way to do this ?