Skip to content

Instantly share code, notes, and snippets.

@sudharsans
Last active December 19, 2022 15:56
Show Gist options
  • Save sudharsans/620628fcc36469360c2981fd9215e5d2 to your computer and use it in GitHub Desktop.
Save sudharsans/620628fcc36469360c2981fd9215e5d2 to your computer and use it in GitHub Desktop.
Groovy, AWS CLI, Active Choices Reactive Parameter and Jenkins
// https://stackoverflow.com/questions/48982349/query-aws-cli-to-populate-jenkins-active-choices-reactive-parameter-linux
// Get list of IPS from AWS CLI and populate Active Choices Reactive Parameter
def command = 'aws ec2 describe-instances --filters Name=tag:Name,Values=Test --query Reservations[*].Instances[*].PrivateIpAddress --output text'
def proc = command.execute()
proc.waitFor()
def output = proc.in.text
def exitcode= proc.exitValue()
def error = proc.err.text
if (error) {
println "Std Err: ${error}"
println "Process exit code: ${exitcode}"
return exitcode
}
//println output.split()
return output.tokenize()
@cabrizio
Copy link

I'm having an issue when I introduce the tag key, like so

def command = 'aws ec2 describe-instances --filters Name=tag:environment,Values=CF-LAB --query 'Reservations[*].Instances[*].{ID:InstanceId,NAME:[Tags[?Key==`Name`].Value[] | [0]]}' --output text'

def proc = command.execute()
proc.waitFor()              

def output = proc.in.text
def exitcode= proc.exitValue()
def error = proc.err.text

if (error) {
	println "Std Err: ${error}"
	println "Process exit code: ${exitcode}"
	return exitcode
}

//println output.split()
return output.tokenize()

It looks like a syntax issue, but I cannot figure out why. any idea?
Thanks

@pankajpandav
Copy link

try this -
def command = 'aws ec2 describe-instances --filters Name=tag:environment,Values=CF-LAB --query \'Reservations[*].Instances[*].{ID:InstanceId,NAME:[Tags[?Key==Name].Value[] | [0]]}\' --output text'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment