That's great, thank you very much. I was able to take your code, substitute MAL for HOST, for instance, and test it in a groovy web console, and it returned the correct value and interpolated the variable even though the variable wasn't defined until after the pathname was defined with the variable in it. Exactly what I was looking for. However...
I took the same code and pasted it into my test jenkins pipeline and now I'm getting an error on a subsequent command which I looked up, but I think it may be over my head... Note that I'm getting the correct result with my substituted variable, but I'm just printing to the console and I'm not using it yet in the sshcommand, and yet sshcommand appears to be failing where before it wasn't.
6:24:05 Executing command on care SOC[xx.xx.xx.xx]: ls -ltr /srv/jboss/server/ sudo: false
16:24:05 something failed
16:24:05 java.io.NotSerializableException: groovy.text.SimpleTemplateEngine$SimpleTemplate
code block:
import java.text.SimpleDateFormat
import groovy.text.SimpleTemplateEngine
node {
def dateFormat = new SimpleDateFormat("yyyyMMddHHmm")
MALarray = params.multi.split(',')
MALarray.each { MAL ->
if (MAL in ['care', 'conn'] ) {
echo 'care or conn'
servers = ['xx.xx.xx.xx']
}
def soc_remote = [:]
soc_remote.allowAnyHosts = true
withCredentials([usernamePassword(credentialsId: 'Credentials', usernameVariable: 'USER_ID', passwordVariable: 'USER_PASSWORD')]) {
soc_remote.user = USER_ID
soc_remote.password = USER_PASSWORD
def templateMessage = new SimpleTemplateEngine().createTemplate(filepath)
def result = { mal -> templateMessage.make(mal: mal)}
println(result(MAL)) // this is working correctly
servers.each { server ->
soc_remote.host = server
try {
echo("MAL is ${MAL}")
sshCommand remote: soc_remote, command: "ls -ltr /srv/jboss/server/"
} catch (err) {
echo "something failed"
echo "${err}" // this is where I'm getting the error on the sshcommand
}
}
}
}
}