Here is a concept which you can follow. Assume that the hosts have been configured as Jenkins slaves already. Assume that hosts are provided in pipeline job parameter HOSTS as whitespace separated list.Below is an example which you can take reference from:
def hosts_pairs = HOSTS.split().collate(2)
for (pair in host_pairs) {
def branches = [:]
for (h in pair) {
def host = h // fresh variable per iteration; it will be mutated
branches[host] = {
stage(host) {
node(host) {
// do the actual job here, e.g.
// execute a shell script
sh "echo hello world"
}
}
}
}
parallel branches
}
I hope the above information would resolve your query.