Add Jenkins Tool Location with ansible

Resently I hat do setup a jenkins slave with ansible. I used some ansible roles from ansible-galaxy, but all did miss to set the tool location.

So I read some java doc and added the tool location config to the ruby-jinja2 template.

import hudson.model.AbstractBuild
import hudson.model.Node
import hudson.plugins.sshslaves.SSHLauncher
import hudson.plugins.sshslaves.verifiers.NonVerifyingKeyVerificationStrategy
import hudson.plugins.sshslaves.verifiers.SshHostKeyVerificationStrategy
import hudson.slaves.ComputerLauncher
import hudson.slaves.DumbSlave
import hudson.util.VariableResolver
import jenkins.model.Jenkins
import hudson.slaves.*
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry
 import hudson.tools.ToolLocationNodeProperty
import hudson.plugins.git.GitTool
import hudson.plugins.git.GitTool$DescriptorImpl
import hudson.tools.ToolInstallation

List env = new ArrayList();
{% for item in (slave_environments | dict3items) %}
env.add(new Entry("{{ item.key }}", "{{ item.value }}"))
{% endfor %}

List toolLoc = new ArrayList();
{% for item in (slave_tool_locations ) %}
toolLoc.add(new ToolLocationNodeProperty.ToolLocation(ToolInstallation.all().get(hudson.plugins.git.GitTool$DescriptorImpl.class), "{{ item.name }}", "{{ item.home }}"))
{% endfor %}

EnvironmentVariablesNodeProperty envPro = new EnvironmentVariablesNodeProperty(env);
ToolLocationNodeProperty envToolLoc = new ToolLocationNodeProperty(toolLoc);

Jenkins jenkins = Jenkins.getInstance()

String nodeHostname = "{{ slave_linux_host }}"
String nodeCredentialID = "{{ slave_linux_jenkins_cred_id }}"
int nodePort = {{ slave_linux_ssh_port }}
SshHostKeyVerificationStrategy strategy = new NonVerifyingKeyVerificationStrategy()

ComputerLauncher nodeLauncher = new SSHLauncher( nodeHostname, nodePort,
nodeCredentialID, null, null, null, null, 31, 20, 10, strategy )

String nodeName = "{{ slave_agent_name }}"
String nodeRemoteFS = "{{ slave_linux_home }}"

Node node = new DumbSlave(nodeName, nodeRemoteFS, nodeLauncher)
node.setNumExecutors({{ slave_executors_num }})
node.setLabelString("{{ slave_linux_labels | join(' ') }}")
node.getNodeProperties().add(envPro)
node.getNodeProperties().add(envToolLoc)
{% if slave_mode_exclusive %}
node.setMode(Node.Mode.EXCLUSIVE)
{% else %}
node.setMode(Node.Mode.NORMAL)
{% endif %}
jenkins.addNode(node) 

If this did help you, consider to send me flower or a donut.