如何在不重启Jenkins master的情况下批量添加Linux节点?
Absolutely, you can automate this tedious task without manually clicking through 100 times—and no Jenkins master restart is needed! Here are two reliable approaches that fit your requirements perfectly:
Method 1: Jenkins CLI + Shell Script
This approach leverages the Jenkins command-line interface to export your template node's config, tweak it for each server, and create nodes in bulk.
Export your template node configuration
First, grab the XML config of your existing "template" node (the one you want to copy):java -jar jenkins-cli.jar -s http://your-jenkins-master-url/ get-node your-template-node-name > template-node.xmlNote: You'll need to download the
jenkins-cli.jarfrom your Jenkins instance's/jnlpJars/jenkins-cli.jarendpoint.Write a shell script to generate new node configs
Create a script (e.g.,create-nodes.sh) that loops through your server list, replaces the necessary values in the template XML, and creates each node:#!/bin/bash JENKINS_URL="http://your-jenkins-master-url/" CLI_JAR="./jenkins-cli.jar" TEMPLATE_CONFIG="./template-node.xml" USERNAME="your-jenkins-username" API_TOKEN="your-jenkins-api-token" # List of your 100 Linux servers (replace with actual hostnames/IPs) SERVERS=( "server-1.your-domain.com" "server-2.your-domain.com" # ... add all 100 servers here ) for SERVER in "${SERVERS[@]}"; do NODE_NAME="node-${SERVER%%.*}" # Extract short name from hostname # Create modified config for this node sed -e "s/your-template-node-name/${NODE_NAME}/" \ -e "s/template-server-host/${SERVER}/" \ "${TEMPLATE_CONFIG}" > "${NODE_NAME}.xml" # Create the node in Jenkins java -jar "${CLI_JAR}" -s "${JENKINS_URL}" \ --username "${USERNAME}" --password "${API_TOKEN}" \ create-node "${NODE_NAME}" < "${NODE_NAME}.xml" echo "Created node: ${NODE_NAME} (host: ${SERVER})" rm "${NODE_NAME}.xml" # Clean up temporary file doneAdjust the
sedreplacements to match fields in your template XML (like labels, SSH credentials ID, etc.) as needed.Run the script
Make it executable and run:chmod +x create-nodes.sh ./create-nodes.sh
Method 2: Groovy Script (Jenkins Script Console or API)
Jenkins runs on Java and supports Groovy scripts directly via the Script Console, which lets you interact with Jenkins' internal API to copy nodes programmatically.
Write the Groovy script
Create a filecopy-nodes.groovywith this code (adjust values to match your environment):// Configuration def templateNodeName = "your-template-node-name" def sshCredentialId = "your-ssh-credential-id" // ID of Jenkins SSH credentials def serverList = [ "server-1.your-domain.com", "server-2.your-domain.com", // ... add all 100 servers here ] // Fetch the template node def templateNode = Jenkins.instance.getNode(templateNodeName) if (!templateNode) { throw new Exception("Template node '${templateNodeName}' not found!") } // Create each new node serverList.each { serverHost -> def nodeName = "node-${serverHost%%.*}" // Skip if node already exists if (Jenkins.instance.getNode(nodeName)) { println "Node '${nodeName}' already exists, skipping..." return } // Copy template node configuration def newNode = templateNode.copy() newNode.name = nodeName // Update SSH launcher settings (assuming your template uses SSH) def launcher = newNode.launcher if (launcher instanceof hudson.plugins.sshslaves.SSHLauncher) { launcher.host = serverHost launcher.credentialsId = sshCredentialId // Optional: adjust port, timeout, etc. if needed // launcher.port = 22 // launcher.connectionTimeout = 30 } // Update labels (customize as needed) newNode.labelString = "linux production ${nodeName}" // Save the new node Jenkins.instance.addNode(newNode) println "Successfully created node '${nodeName}' for host '${serverHost}'" } println "Node creation process completed!"Execute the script
- Via Jenkins Script Console: Go to
Manage Jenkins > Script Console, paste the script, and click "Run". - Via API (for remote execution): Use
curlto run the script without logging into the UI:curl -X POST -u your-username:your-api-token \ http://your-jenkins-master-url/script \ --data-urlencode "script=$(cat copy-nodes.groovy)"
- Via Jenkins Script Console: Go to
Notes to Keep in Mind
- Test first: Run the automation for 1-2 nodes first to confirm the configuration is correct before scaling to 100.
- Backup Jenkins config: Before making bulk changes, back up your Jenkins home directory or export the system config via
Manage Jenkins > System Backup. - Permissions: Ensure the user executing the script/CLI has the
Create nodespermission in Jenkins. - SSH Credentials: If using SSH launchers, make sure the specified credential ID exists in Jenkins and has access to all target servers.
内容的提问来源于stack exchange,提问作者Ronalkiha

