You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不重启Jenkins master的情况下批量添加Linux节点?

Automating Jenkins Node Creation via "Copy Existing Node"

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.

  1. 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.xml
    

    Note: You'll need to download the jenkins-cli.jar from your Jenkins instance's /jnlpJars/jenkins-cli.jar endpoint.

  2. 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
    done
    

    Adjust the sed replacements to match fields in your template XML (like labels, SSH credentials ID, etc.) as needed.

  3. 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.

  1. Write the Groovy script
    Create a file copy-nodes.groovy with 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!"
    
  2. 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 curl to 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)"
      

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 nodes permission 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:44:50