{"id":1002,"date":"2020-04-18T16:00:23","date_gmt":"2020-04-18T14:00:23","guid":{"rendered":"http:\/\/www.netexpertise.eu\/en\/?p=1002"},"modified":"2021-05-13T20:21:22","modified_gmt":"2021-05-13T18:21:22","slug":"jenkins-slave-auto-register-with-rest-api","status":"publish","type":"post","link":"http:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html","title":{"rendered":"Auto Register a Jenkins Slave with REST API"},"content":{"rendered":"\n<p>Here is how to create a <a href=\"\/en\/category\/devops\/jenkins\">Jenkins<\/a> node that registers automatically with a REST API call on the master. Most of the work is to build some json code that describes the new Jenkins slave. The configuration can be slightly different depending on the node settings you are willing to apply. To get exactly what you want, you may create a dummy slave manually and capture the JSON object in your browser developer tool&#8217;s network tab while you click on &#8220;Save&#8221;. In the meantime, here is mine, assuming agent launches with a startup script:<br><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n   \"name\": \"my_jenkins_slave\",\n   \"nodeDescription\": \"my Jenkins slave\",\n   \"numExecutors\": \"2\",\n   \"remoteFS\": \"\/home\/jenkins\",\n   \"labelString\": \"slave\",\n   \"mode\": \"EXCLUSIVE\",\n   \"\": [\n      \"hudson.slaves.JNLPLauncher\",\n      \"hudson.slaves.RetentionStrategy$Always\"\n   ],\n   \"launcher\": {\n      \"stapler-class\": \"hudson.slaves.JNLPLauncher\",\n      \"$class\": \"hudson.slaves.JNLPLauncher\",\n      \"workDirSettings\": {\n         \"disabled\": true,\n         \"workDirPath\": \"\",\n         \"internalDir\": \"remoting\",\n         \"failIfWorkDirIsMissing\": false\n      },\n      \"tunnel\": \"\",\n      \"vmargs\": \"\"\n   },\n   \"retentionStrategy\": {\n      \"stapler-class\": \"hudson.slaves.RetentionStrategy$Always\",\n      \"$class\": \"hudson.slaves.RetentionStrategy$Always\"\n   },\n   \"nodeProperties\": {\n      \"stapler-class-bag\": \"true\",\n      \"hudson-slaves-EnvironmentVariablesNodeProperty\": {\n         \"env\": [\n            {\n               \"key\": \"JAVA_HOME\",\n               \"value\": \"\/usr\/lib\/jvm\/java-8-openjdk-amd64\"\n            }\n         ]\n      },\n      \"_comment:\": {\n         \"hudson-tools-ToolLocationNodeProperty\": {\n           \"locations\": [\n               {\n                  \"key\": \"hudson.model.JDK$DescriptorImpl@JAVA-8\",\n                  \"home\": \"\/usr\/bin\/java\"\n               }\n            ]\n         }\n      }\n   }\n}<\/code><\/pre>\n\n\n\n<p><br>All you need is define the 3 following environment variables in the shell:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">jenkins_user=my_jenkins_user\njenkins_token=my_jenkins_token\njenkins_url=https:\/\/jenkins.mydomain.lan<\/code><\/pre>\n\n\n\n<p><br>And post above JSON file with a simple curl:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ curl -L -s -o \/dev\/null -w \"%{http_code}\" -u $jenkins_user:$jenkins_token \\\n-H \"Content-Type:application\/x-www-form-urlencoded\" -X POST \\\n-d \"json=$(cat \/tmp\/node.json)\" \\\n\"$jenkins_url\/computer\/doCreateItem?name=my_jenkins_slave&amp;type=hudson.slaves.DumbSlave\"; \\\necho\n200<\/code><\/pre>\n\n\n\n<p><br>If you do not get a 200 response code, run the same skipping &#8220;-o \/dev\/null&#8221; to troubleshoot.<br>Some say you may do the same with different tools like Jenkins CLI or some plugins but Jenkins REST API works through any firewall and is pretty easy to set up.<br>In the next post, I&#8217;ll use this method to automate <a href=\"\/en\/devops\/ansible\/register-a-jenkins-slave-with-ansible.html\">Jenkins agent registration with Ansible<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is how to create a Jenkins node that registers automatically with a REST API call on the master. Most of the work is to build some json code that describes the new Jenkins slave. The configuration can be slightly different depending on the node settings you are willing to apply. To get exactly what [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[426],"tags":[428],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Netexpertise - Auto Register a Jenkins Slave with REST API<\/title>\n<meta name=\"description\" content=\"Register a Jenkins slave with a JSON configuration file and curl calling Jenkins master through a REST API. Next, automate it with Ansible\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Netexpertise - Auto Register a Jenkins Slave with REST API\" \/>\n<meta property=\"og:description\" content=\"Register a Jenkins slave with a JSON configuration file and curl calling Jenkins master through a REST API. Next, automate it with Ansible\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html\" \/>\n<meta property=\"og:site_name\" content=\"Netexpertise\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-18T14:00:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-13T18:21:22+00:00\" \/>\n<meta name=\"author\" content=\"dave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@netexpertise\" \/>\n<meta name=\"twitter:site\" content=\"@netexpertise\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html\",\"url\":\"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html\",\"name\":\"Netexpertise - Auto Register a Jenkins Slave with REST API\",\"isPartOf\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#website\"},\"datePublished\":\"2020-04-18T14:00:23+00:00\",\"dateModified\":\"2021-05-13T18:21:22+00:00\",\"author\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\"},\"description\":\"Register a Jenkins slave with a JSON configuration file and curl calling Jenkins master through a REST API. Next, automate it with Ansible\",\"breadcrumb\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.netexpertise.eu\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Auto Register a Jenkins Slave with REST API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#website\",\"url\":\"https:\/\/www.netexpertise.eu\/en\/\",\"name\":\"Netexpertise\",\"description\":\"Systems \/ Networks \/ DevOps\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.netexpertise.eu\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\",\"name\":\"dave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"caption\":\"dave\"},\"sameAs\":[\"http:\/\/www.netexpertise.eu\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Netexpertise - Auto Register a Jenkins Slave with REST API","description":"Register a Jenkins slave with a JSON configuration file and curl calling Jenkins master through a REST API. Next, automate it with Ansible","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html","og_locale":"en_US","og_type":"article","og_title":"Netexpertise - Auto Register a Jenkins Slave with REST API","og_description":"Register a Jenkins slave with a JSON configuration file and curl calling Jenkins master through a REST API. Next, automate it with Ansible","og_url":"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html","og_site_name":"Netexpertise","article_published_time":"2020-04-18T14:00:23+00:00","article_modified_time":"2021-05-13T18:21:22+00:00","author":"dave","twitter_card":"summary_large_image","twitter_creator":"@netexpertise","twitter_site":"@netexpertise","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html","url":"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html","name":"Netexpertise - Auto Register a Jenkins Slave with REST API","isPartOf":{"@id":"https:\/\/www.netexpertise.eu\/en\/#website"},"datePublished":"2020-04-18T14:00:23+00:00","dateModified":"2021-05-13T18:21:22+00:00","author":{"@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa"},"description":"Register a Jenkins slave with a JSON configuration file and curl calling Jenkins master through a REST API. Next, automate it with Ansible","breadcrumb":{"@id":"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.netexpertise.eu\/en\/devops\/jenkins\/jenkins-slave-auto-register-with-rest-api.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.netexpertise.eu\/en"},{"@type":"ListItem","position":2,"name":"Auto Register a Jenkins Slave with REST API"}]},{"@type":"WebSite","@id":"https:\/\/www.netexpertise.eu\/en\/#website","url":"https:\/\/www.netexpertise.eu\/en\/","name":"Netexpertise","description":"Systems \/ Networks \/ DevOps","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.netexpertise.eu\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa","name":"dave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","caption":"dave"},"sameAs":["http:\/\/www.netexpertise.eu"]}]}},"_links":{"self":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/1002"}],"collection":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/comments?post=1002"}],"version-history":[{"count":0,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/1002\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/media?parent=1002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/categories?post=1002"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/tags?post=1002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}