-
Notifications
You must be signed in to change notification settings - Fork 7
/
jenkins.yml
273 lines (236 loc) · 7.79 KB
/
jenkins.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# vim: ft=ansible
---
########################
# Deploy Jenkins Master
########################
- hosts: jenkins_master
become: true
tags:
- jenkins_master
vars_files:
- "{{ inventory_dir }}/{{ vars_files_relative }}/vars/main.yml"
- ~/.ansible/vars/toad_vars.yml
roles:
- { role: 'franklinkim.sudo' }
- { role: 'geerlingguy.repo-epel' }
- { role: 'geerlingguy.nginx' }
- { role: 'leifmadsen.jenkins' }
vars:
sudo_defaults:
- defaults: '!requiretty'
pre_tasks:
- name: Add jenkins service unit to systemd in case of docker.
template:
src: jenkins_systemd.j2
dest: /etc/systemd/system/jenkins.service
owner: root
group: root
mode: "0655"
when: deploy_type is defined and deploy_type == 'docker'
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 }}"
# In case of docker, jenkins is managed under systemd unit (see above)
- name: jenkins init script is removed in case of docker
file:
path: /etc/init.d/jenkins
state: absent
when: deploy_type is defined and deploy_type == 'docker'
- name: Install python-pip
package:
name: python-pip
state: present
post_tasks:
# Operating system configuration and setup
- name: Add jenkins user to wheel group
user:
name: jenkins
home: /var/lib/jenkins
groups: wheel
append: yes
shell: /bin/bash
- name: Update ownership and perms for shadow
file:
group: wheel
mode: "0040"
path: /etc/shadow
- name: Create admin user in system
user:
name: "{{ jenkins_admin_username }}"
password: "{{ jenkins_admin_password |password_hash('sha512')}}"
shell: /bin/bash
- name: Ensure libsemanage-python is installed
package:
name: libsemanage-python
state: present
- name: Validate SELinux is enabled
selinux:
policy: targeted
state: permissive
when: ansible_connection != 'docker'
- name: SELinux -- Enable httpd_can_network_connect
seboolean:
name: httpd_can_network_connect
state: yes
persistent: yes
when: ansible_connection != 'docker'
# Copy in configuration for Nginx web proxy fronting Jenkins web UI
- name: Copy Jenkins Nginx Configuration
template:
src: jenkins_master-vhost.conf.j2
dest: /etc/nginx/conf.d/jenkins_master.conf
owner: root
group: root
mode: "0644"
notify: restart nginx
- name: Automate jenkins configuration
template:
src: "{{item.src}}"
dest: "{{item.dest}}"
owner: "{{item.owner}}"
group: "{{item.group}}"
mode: "{{item.mode}}"
notify: restart jenkins
with_items:
- src: jenkins_config/config.xml.j2
dest: /var/lib/jenkins/config.xml
owner: jenkins
group: jenkins
mode: "0644"
- src: jenkins_config/credentials.xml.j2
dest: /var/lib/jenkins/credentials.xml
owner: jenkins
group: jenkins
mode: "0644"
- src: jenkins_config/be.certipost.hudson.plugin.SCPRepositoryPublisher.xml.j2
dest: /var/lib/jenkins/be.certipost.hudson.plugin.SCPRepositoryPublisher.xml
owner: jenkins
group: jenkins
mode: "0644"
# Create directory where results from slave workspaces will be copied to.
- name: Create slave results directory
file:
path: "{{ jenkins_master_results_directory }}"
state: directory
owner: jenkins
group: jenkins
mode: "0755"
- include: rhel_register.yml
when: ansible_os_family == "RedHat" and master_subscribe_rhn|bool == True
- include: rhel_mirror_sync.yml
when: ansible_os_family == "RedHat" and master_mirror_sync|bool == true
#######################
# Deploy Jenkins Slave
#######################
- hosts: jenkins_slave
tags:
- jenkins_slave
become: yes
roles:
- { role: 'franklinkim.sudo' }
- { role: 'geerlingguy.java' }
vars_files:
- "{{ inventory_dir }}/{{ vars_files_relative }}/vars/main.yml"
- ~/.ansible/vars/toad_vars.yml
vars:
sudo_defaults:
- defaults: '!requiretty'
sudo_users:
- name: 'stack'
nopasswd: yes
- name: 'ansible'
nopasswd: yes
pre_tasks:
- name: Ensure libselinux-python is installed
package:
name: libselinux-python
state: present
- name: Create stack user
user:
name: stack
comment: "Stack Slave User"
generate_ssh_key: yes
tasks:
- name: Validate Git is installed
package:
name: git
state: present
- name: Get hostname of slave
shell: echo $HOSTNAME
register: jenkins_slave_ssh_hostname
tags:
- skip_ansible_lint
- jenkins_ssh
- name: Create log directory for images
file:
path: /var/log/artib
state: directory
owner: stack
group: stack
mode: "0755"
become: true
- include: rhel_register.yml
when: ansible_os_family == "RedHat" and slave_subscribe_rhn|bool == True
- include: rhel_mirror_sync.yml
when: ansible_os_family == "RedHat" and slave_mirror_sync|bool == true
########################
# Setup SSH connections
########################
- include: jenkins_ssh.yml
###############################
# Add the slaves to the master
###############################
- hosts: jenkins_master
become: yes
tags:
- jenkins_slave
vars_files:
- ~/.ansible/vars/toad_vars.yml
tasks:
- name: Manage jenkins slaves
block:
- name: Check if our node already exists
command: >
java -jar {{ jenkins_jar_location }} -s http://{{ jenkins_hostname }}:{{ jenkins_http_port }}{{ jenkins_url_prefix | default('') }}/
get-node {{ item }}
--username {{ jenkins_admin_username }}
--password {{ jenkins_admin_password }}
ignore_errors: true
register: jenkins_cli_get_nodes
changed_when: false
with_items:
- "{{ groups['jenkins_slave'] }}"
- name: Deploy template for Jenkins slave node
template:
src: jenkins_slave.j2
dest: /tmp/jenkins_slave_{{ item.item }}.tmpl
when: jenkins_cli_get_nodes is defined and "'jenkins_slave' in groups" and "{{ item.rc }}" != '0'
with_items:
- "{{ jenkins_cli_get_nodes.results }}"
- 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 {{ item.item }}
--username {{ jenkins_admin_username }}
--password {{ jenkins_admin_password }} < /tmp/jenkins_slave_{{ item.item }}.tmpl
when: jenkins_cli_get_nodes is defined and "{{ item.rc }}" != '0'
with_items:
- "{{ jenkins_cli_get_nodes.results }}"
- name: Remove template for Jenkins slave node
file:
name: /tmp/jenkins_slave_{{ item }}.tmpl
state: absent
when: jenkins_cli_get_nodes is defined and "{{ item.rc }}" != '0'
with_items:
- "{{ jenkins_cli_get_nodes.results }}"
when: "'jenkins_slave' in groups"
######################
# Deploy Jenkins jobs
######################
- include: jenkins_jobs.yml