Home > OS >  Active Choice Parameter with Azure VM LIST
Active Choice Parameter with Azure VM LIST

Time:09-20

I'm trying to create a VM list out of active choice parameter:

def command = 'az vm list --resource-group test-test-test \
               --query '[].{computerName:osProfile.computerName}' \
               --output tsv'
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.split()

How am I supposed to write the groovy script inside the Active choice parameter in Jenkins? I just want to fetch all azure VMS in a list and send them to active choice parameter. One more question does the active choice paramter authenticate only on master? Can it authenticate on a node that has the AZ binary

CodePudding user response:

Yes the script will always run on the Master and it can be included like shown below.

properties([
    parameters([
        [$class: 'ChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            description: 'Select the Host', 
            name: 'Host',
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return [\'Could not get Host\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        '''
                        def command = 'az vm list --resource-group dhl-dhlcom-bbb \
                                      --query '[].{computerName:osProfile.computerName}' \
                                        --output tsv'
                         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
                         }

                        return output.split()
                        '''
                ]
            ]
        ]
    ])
])
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script {
                    echo "Host:::: ${params.Host}"
                }
            }
        }
    }
}
  • Related