From d4c9205da455aae8e3aa08fea02af6f09afc8ac1 Mon Sep 17 00:00:00 2001 From: Leif Madsen Date: Mon, 15 Aug 2016 17:05:09 -0400 Subject: [PATCH] Add initial support for adding Jenkins slaves --- jenkins.yml | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/jenkins.yml b/jenkins.yml index ec98ef2..c40058e 100644 --- a/jenkins.yml +++ b/jenkins.yml @@ -19,6 +19,16 @@ sudo_defaults: - defaults: '!requiretty' + tasks: + - name: Set facts for later use + set_fact: + jenkins_jar_location: "{{ jenkins_jar_location }}" + jenkins_hostname: "{{ jenkins_hostname }}" + jenkins_http_port: "{{ jenkins_http_port }}" + jenkins_url_prefix: "{{ jenkins_url_prefix }}" + jenkins_admin_username: "{{ jenkins_admin_username }}" + jenkins_admin_password: "{{ jenkins_admin_password }}" + post_tasks: - name: Add jenkins user to wheel group user: @@ -253,3 +263,45 @@ authorized_key: user: root key: "{{ jenkins_slave_pub.stdout }}" + +- hosts: jenkins_master + become: yes + tags: + - jenkins_slave_test + + vars_files: + - ~/.ansible/vars/cira_vars.yml + + tasks: + # TODO: This is kind of hacky since it doesn't deal with multiple jenkins + # slaves. We'll need to circle back and make this deal with lists. + - name: Check if our node already exists + ignore_errors: true + command: > + java -jar {{ jenkins_jar_location }} -s http://{{ jenkins_hostname }}:{{ jenkins_http_port }}{{ jenkins_url_prefix | default('') }}/ + get-node {{ slave_name }} + --username {{ jenkins_admin_username }} + --password {{ jenkins_admin_password }} + register: jenkins_cli_get_node + + - debug: var=jenkins_cli_get_node + + - name: Deploy template for Jenkins slave node + template: + src: jenkins_slave.j2 + dest: /tmp/jenkins_slave_{{ slave_name }}.tmpl + when: jenkins_cli_get_node.stderr is defined and jenkins_cli_get_node.stderr != "" + + - name: Add Jenkins slave node to the master + shell: > + java -jar {{ jenkins_jar_location }} -s http://{{ jenkins_hostname }}:{{ jenkins_http_port }}{{ jenkins_url_prefix | default('') }}/ + create-node {{ slave_name }} + --username {{ jenkins_admin_username }} + --password {{ jenkins_admin_password }} < /tmp/jenkins_slave_{{ slave_name }}.tmpl + when: jenkins_cli_get_node.stderr is defined and jenkins_cli_get_node.stderr != "" + + - name: Remove template for Jenkins slave node + file: + name: /tmp/jenkins_slave_{{ slave_name }}.tmpl + state: absent + when: slave_name is defined and jenkins_cli_get_node.stderr != ""