Skip to content

Commit c2652e6

Browse files
nre-abletonemmetog
authored andcommitted
Add support for HTTPS, improve handling of Jenkins URL (#48)
* Move duplicated tasks to common file * Set the jenkins_url fact based on protocol This change un-defines jenkins_url in the default's main.yml file, and instead allows users to define jenkins_hostname instead. The rationale for this is: - Previously jenkins_url was not a true URL as it did not include the port. This was added by the role as necessary. - It required the user to specify the protocol, which became a bit awkward when adding support for HTTPS. This change includes backwards-compatibility support for playbooks which define jenkins_url by re-defining it to the new value with a port, and displaying a debug warning message to the user. * Add support for HTTPS * Improve "Role Variables" section of readme This section originally copy/pasted the defaults/main.yml file, which is not very sustainable since changes in that file were not always propagated to the README. Instead, this section now includes documentation on the most relevant options and links to the actual file for everything else.
1 parent a07b590 commit c2652e6

14 files changed

+180
-138
lines changed

README.md

Lines changed: 51 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -39,91 +39,35 @@ $ ansible-galaxy install emmetog.jenkins
3939
Role Variables
4040
--------------
4141

42-
```yml
43-
jenkins_version: "2.73.1" # The exact version of jenkins to deploy
44-
45-
jenkins_url: "http://127.0.0.1" # The url that Jenkins will be accessible on
46-
jenkins_port: "8080" # The port that Jenkins will listen on
47-
jenkins_home: /data/jenkins # The directory on the server where the Jenkins configs will live
48-
jenkins_admin: "admin@example.com" # The admininstrator email address for the Jenkins server
49-
50-
# If you need to override any java options then do that here.
51-
jenkins_java_opts: "-Djenkins.install.runSetupWizard=false"
52-
53-
# Install Jenkins by means of a Docker container
54-
jenkins_install_via: "docker"
55-
56-
# Install Jenkins directly on Ubuntu/Debian Linux systems
57-
jenkins_install_via: "apt"
58-
59-
# Install Jenkins directly on RedHat/CentOS Linux systems
60-
jenkins_install_via: "yum"
61-
62-
# Configuration files owner and group
63-
jenkins_config_owner: "ubuntu"
64-
jenkins_config_group: "ubuntu"
65-
66-
# The locations of the configuration files for jenkins
67-
jenkins_source_dir_configs: "{{ playbook_dir }}/jenkins-configs"
68-
jenkins_source_dir_jobs: "{{ jenkins_source_dir_configs }}/jobs"
69-
70-
# config.xml template source
71-
jenkins_source_config_xml: "{{ jenkins_source_dir_configs }}/config.xml"
42+
The following variables influence how Jenkins is installed:
7243

73-
# Include custom files for jenkins installation
74-
jenkins_include_custom_files: false
75-
jenkins_custom_files: {}
44+
- `jenkins_install_via`: Controls how Jenkins is installed. **Important**: This
45+
variable must be defined to one of the following values:
46+
- `docker`: Install in a Docker container
47+
- `apt`: Install Jenkins directly on Ubuntu/Debian Linux systems
48+
- `yum`: Install Jenkins directly on RedHat/CentOS Linux systems
49+
- `jenkins_version`: The exact version of jenkins to install
7650

77-
# Include secrets directory during installation
78-
jenkins_include_secrets: false
79-
jenkins_source_secrets: "{{ jenkins_source_dir_configs }}/secrets/"
51+
The following variables influence how Jenkins is configured:
8052

81-
# The names of the jobs (config.xml must exist under jenkins_source_dir_jobs/job_name/)
82-
jenkins_jobs: []
53+
- `jenkins_url`: The URL that Jenkins will be accessible on
54+
- `jenkins_port`: The port that Jenkins will listen on
55+
- `jenkins_home`: The directory on the server where the Jenkins configs will
56+
live
57+
- `jenkins_admin`: The administrator's email address for the Jenkins server
58+
- `jenkins_java_opts`: Options passed to the Java executable
59+
- `jenkins_config_owner`: Owner of Jenkins configuration files
60+
- `jenkins_config_group`: Group of Jenkins configuration files
8361

84-
# These plugins will be installed in the jenkins instance
85-
jenkins_plugins:
86-
- git
87-
- log-parser
88-
- copyartifact
89-
- workflow-aggregator
90-
- workflow-multibranch
91-
- docker-workflow
92-
- template-project
93-
- ec2
62+
The following list variables influence the jobs/plugins that will be installed
63+
in Jenkins:
9464

95-
# List of sources of custom jenkins plugins to install
96-
jenkins_custom_plugins: []
65+
- `jenkins_jobs`: List of names of the jobs to copy to Jenkins. The `config.xml`
66+
file must exist under `jenkins_source_dir_jobs/<job_name>`
67+
- `jenkins_plugins`: List of plugin IDs to install on Jenkins.
68+
- `jenkins_custom_plugins`: List of custom plugins to install on Jenkins.
9769

98-
###################################################
99-
# Docker vars: apply to deploying via docker only #
100-
###################################################
101-
102-
# The docker hub image name
103-
jenkins_docker_image: "jenkins/jenkins"
104-
105-
# Configs specific to the "docker" method of running jenkins
106-
# The name of the jenkins container
107-
jenkins_docker_container_name: jenkins
108-
109-
# Default, if true, the port will be exposed on the host (using "port")
110-
# If set to false, the port will only be exposed to other containers (using "expose")
111-
jenkins_docker_expose_port: true
112-
113-
114-
#############################################
115-
# Apt vars: apply to deploying via apt only #
116-
#############################################
117-
118-
# Packages which are to be installed on the jenkins instance
119-
jenkins_apt_packages:
120-
- openjdk-8-jdk
121-
122-
# Java version to use. Note that JDK 8 is required for Jenkins
123-
# 2.54 or greater.
124-
jenkins_java_version: "java-1.8.0-openjdk-amd64"
125-
126-
```
70+
For a complete list of variables, see [`defaults/main.yml`](defaults/main.yml).
12771

12872
Example Playbook
12973
----------------
@@ -133,7 +77,7 @@ Example Playbook
13377

13478
vars:
13579
jenkins_version: "2.73.1"
136-
jenkins_url: http://jenkins.example.com
80+
jenkins_hostname: "jenkins.example.com"
13781
jenkins_port: 80
13882
jenkins_install_via: "docker"
13983
jenkins_jobs:
@@ -154,18 +98,33 @@ Example Playbook
15498
HTTPS
15599
-----
156100
157-
If you want to enable HTTPS on jenkins we recommend that you use a
158-
reverse proxy like [jwilder/nginx-proxy](https://github.com/jwilder/nginx-proxy)
159-
or [traefik](https://github.com/containous/traefik) and configure it
160-
as the HTTPS endpoint instead of configuring jenkins itself with HTTPS.
161-
This gives you more flexibility and better separation of concerns. See
162-
the documentation in those projects for more details on how to deploy
163-
the proxies and configure HTTPS.
164-
165-
If using a reverse proxy in front of the jenkins
166-
instance and deploying using docker you probably
167-
want to set the `jenkins_docker_expose_port` var to false so that the
168-
port is not exposed on the host, only to the reverse proxy.
101+
If you want to enable HTTPS on Jenkins, this can be done as follows:
102+
103+
- Define `jenkins_port_https` to the port that Jenkins should listen on
104+
- Define variables *either* for the JKS keystore or the CA signed certificate:
105+
* For JKS keystore, you'll need to define:
106+
- `jenkins_https_keystore`: Path to the keystore file on the control host,
107+
which will be copied to the Jenkins server by this role.
108+
- `jenkins_https_keystore_password`: Password for said JKS keystore. Use of
109+
the Ansible vault is recommended for this.
110+
* For a CA signed certificate file, you'll need to define:
111+
- `jenkins_https_certificate`: Path to the certificate file, which will be
112+
copied to the Jenkins server by this role.
113+
- `jenkins_https_private_key`: Private key for said CA signed certificate.
114+
Use of the Ansible vault is recommended for this.
115+
- Optionally, `jenkins_https_validate_certs` should be defined to `false` if
116+
you are using a self-signed certificate.
117+
118+
If you are deploying Jenkins with Docker, then using a reverse proxy such as
119+
[jwilder/nginx-proxy](https://github.com/jwilder/nginx-proxy) or
120+
[traefik](https://github.com/containous/traefik) is recommended instead of
121+
configuring Jenkins itself. This gives a bit more flexibility and allows for
122+
separation of responsibilities. See the documentation in those projects for
123+
more details on how to deploy the proxies and configure HTTPS.
124+
125+
If using a reverse proxy in front of the Jenkins instance and deploying using
126+
Docker you probably want to set the `jenkins_docker_expose_port` variable to
127+
false so that the port is not exposed on the host, only to the reverse proxy.
169128

170129
Authentication and Security
171130
---------------------------

defaults/main.yml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
---
22
jenkins_version: "2.73.1" # The exact version of jenkins to deploy
33

4-
jenkins_url: "http://127.0.0.1" # The url that Jenkins will be accessible on
5-
jenkins_port: "8080" # The port that Jenkins will listen on
4+
jenkins_hostname: "127.0.0.1" # The hostname that Jenkins will be accessible on
5+
# The port that Jenkins will listen on for unsecured (HTTP) requests. Define to -1 to
6+
# disable HTTP.
7+
jenkins_port: "8080"
8+
# The port that Jenkins will listen on for secured (HTTPS) requests. Define to -1 to
9+
# disable HTTPS. Enabling this option requires a SSL certificate (see below).
10+
jenkins_port_https: "-1"
611
jenkins_home: /data/jenkins # The directory on the server where the Jenkins configs live
712
jenkins_admin: "admin@example.com" # The administrator email address for the server
813

@@ -72,6 +77,26 @@ jenkins_api_token: ""
7277
# Username which owns the above API token.
7378
jenkins_api_username: ""
7479

80+
#########################################
81+
# SSL vars: apply to Jenkins HTTPS only #
82+
#########################################
83+
84+
# See https://wiki.jenkins.io/display/JENKINS/Starting+and+Accessing+Jenkins for more info
85+
86+
# Jenkins JKS keystore file. Mutually exclusive with the certificate/private key options.
87+
jenkins_https_keystore: ""
88+
# Jenkins JKS keystore password.
89+
jenkins_https_keystore_password: ""
90+
91+
# Jenkins CA signed certificate file. Mutually exclusive with the keystore options.
92+
jenkins_https_certificate: ""
93+
# Jenkins CA signed certificate private key.
94+
jenkins_https_private_key: ""
95+
96+
# Set to false if you are using a self-signed certificate and wish to ignore any
97+
# certificate verification errors from Ansible.
98+
jenkins_https_validate_certs: true
99+
75100
###################################################
76101
# Docker vars: apply to deploying via docker only #
77102
###################################################
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version='1.0' encoding='UTF-8'?>
22
<jenkins.model.JenkinsLocationConfiguration>
33
<adminAddress>{{ jenkins_admin }}</adminAddress>
4-
<jenkinsUrl>{{ jenkins_url }}:{{ jenkins_port }}</jenkinsUrl>
4+
<jenkinsUrl>{{ jenkins_url }}</jenkinsUrl>
55
</jenkins.model.JenkinsLocationConfiguration>

tasks/apt/install.yml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,3 @@
6363
name: "jenkins={{ jenkins_version }}"
6464
force: true
6565
update_cache: true
66-
67-
- name: Set JENKINS_HOME
68-
lineinfile:
69-
dest: /etc/default/jenkins
70-
line: "JENKINS_HOME={{ jenkins_home }}"
71-
regexp: '^JENKINS_HOME='
72-
73-
- name: Set Jenkins port
74-
lineinfile:
75-
dest: /etc/default/jenkins
76-
regexp: '^HTTP_PORT='
77-
line: "HTTP_PORT={{ jenkins_port }}"
78-
79-
- name: Set Jenkins command line options
80-
lineinfile:
81-
dest: /etc/default/jenkins
82-
regexp: '^JAVA_ARGS='
83-
line: "JAVA_ARGS=\"{{ jenkins_java_opts }}\""

tasks/cancel-quiet-mode.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
- name: Cancel quiet mode with API token
33
uri:
4-
url: "{{ jenkins_url }}:{{ jenkins_port }}/cancelQuietDown"
4+
url: "{{ jenkins_url }}/cancelQuietDown"
55
method: POST
66
headers:
77
Content-Type: "text/xml"
@@ -16,7 +16,7 @@
1616

1717
- name: Cancel quiet mode with crumb
1818
uri:
19-
url: "{{ jenkins_url }}:{{ jenkins_port }}/cancelQuietDown"
19+
url: "{{ jenkins_url }}/cancelQuietDown"
2020
method: POST
2121
headers:
2222
Content-Type: "text/xml"
@@ -27,9 +27,10 @@
2727

2828
- name: Cancel quiet mode with no security
2929
uri:
30-
url: "{{ jenkins_url }}:{{ jenkins_port }}/cancelQuietDown"
30+
url: "{{ jenkins_url }}/cancelQuietDown"
3131
method: POST
3232
headers:
3333
Content-Type: "text/xml"
3434
status_code: 200,302
35+
validate_certs: "{{ jenkins_https_validate_certs }}"
3536
when: jenkins_auth == "none"

tasks/configure-config.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
- name: Set JENKINS_HOME
3+
lineinfile:
4+
create: true
5+
dest: "/etc/default/jenkins"
6+
line: "JENKINS_HOME={{ jenkins_home }}"
7+
regexp: '^JENKINS_HOME='
8+
state: present
9+
10+
- name: Set Jenkins port
11+
lineinfile:
12+
dest: /etc/default/jenkins
13+
regexp: '^HTTP_PORT='
14+
line: "HTTP_PORT={{ jenkins_port }}"
15+
16+
- name: Set Jenkins Java command line options
17+
lineinfile:
18+
dest: /etc/default/jenkins
19+
regexp: '^JAVA_ARGS='
20+
line: "JAVA_ARGS=\"{{ jenkins_java_opts }}\""
21+
22+
- name: Copy JKS keystore credentials
23+
copy:
24+
src: "{{ jenkins_https_keystore }}"
25+
dest: "{{ jenkins_home }}"
26+
when: jenkins_https_keystore and jenkins_https_keystore_password
27+
28+
- name: Copy CA signed certificate
29+
copy:
30+
src: "{{ jenkins_https_certificate }}"
31+
dest: "{{ jenkins_home }}"
32+
when: jenkins_https_certificate and jenkins_https_private_key
33+
34+
- name: Initialize HTTPS credentials fact
35+
set_fact:
36+
jenkins_https_creds: ""
37+
38+
- name: Set JKS keystore credentials
39+
set_fact:
40+
jenkins_https_creds: >
41+
--httpsKeyStore='{{ jenkins_home }}/{{ jenkins_https_keystore }}' \
42+
--httpsKeyStorePassword='{{ jenkins_https_keystore_password }}'
43+
when: jenkins_https_keystore and jenkins_https_keystore_password
44+
45+
- name: Set CA signed certificate credentials
46+
set_fact:
47+
jenkins_https_creds: >
48+
--httpsCertificate='{{ jenkins_home }}/{{ jenkins_https_certificate }}' \
49+
--httpsPrivateKey='{{ jenkins_https_private_key }}'
50+
when: jenkins_https_certificate and jenkins_https_private_key
51+
52+
- name: Set Jenkins command line options
53+
lineinfile:
54+
dest: /etc/default/jenkins
55+
regexp: '^JENKINS_ARGS='
56+
line: "JENKINS_ARGS=\"--webroot=/var/cache/$NAME/war --httpPort={{ jenkins_port }} \
57+
--httpsPort={{ jenkins_port_https }} {{ jenkins_https_creds }}\""

tasks/configure-plugins.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
jenkins_home: "{{ jenkins_home }}"
2222
owner: "{{ jenkins_config_owner }}"
2323
group: "{{ jenkins_config_group }}"
24-
url: "{{ jenkins_url }}:{{ jenkins_port }}"
24+
url: "{{ jenkins_url }}"
2525
timeout: "{{ jenkins_plugin_timeout }}"
2626
url_username: "{{ jenkins_api_username }}"
2727
url_password: "{{ jenkins_api_token }}"
2828
force_basic_auth: true
29+
validate_certs: "{{ jenkins_https_validate_certs }}"
2930
with_items: "{{ jenkins_plugins }}"
3031
when: jenkins_auth == "api"
3132

@@ -39,7 +40,7 @@
3940
jenkins_home: "{{ jenkins_home }}"
4041
owner: "{{ jenkins_config_owner }}"
4142
group: "{{ jenkins_config_group }}"
42-
url: "{{ jenkins_url }}:{{ jenkins_port }}"
43+
url: "{{ jenkins_url }}"
4344
timeout: "{{ jenkins_plugin_timeout }}"
4445
with_items: "{{ jenkins_plugins }}"
4546
when: jenkins_auth == "crumb" or jenkins_auth == "none"

tasks/get-crumb.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
---
22
- name: Get crumb for Jenkins API
33
uri:
4-
url: '{{ jenkins_url }}:{{ jenkins_port }}
4+
url: '{{ jenkins_url }}
55
/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'
66
return_content: true
77
status_code: 200,404
8+
validate_certs: "{{ jenkins_https_validate_certs }}"
89
register: jenkins_crumb
910
until: jenkins_crumb.status == 200 or jenkins_crumb.status == 404
1011
retries: 5

tasks/main.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
---
22
- include: "sanity-checks.yml"
33

4+
# Previous versions of this role defined jenkins_url as a default variable, but this URL
5+
# did not include the port, and required the user to hard-code the protocol.
6+
- name: Set jenkins_url fact for backwards-compatibility installations
7+
set_fact:
8+
jenkins_url: "{{ jenkins_url }}:{{ jenkins_port }}"
9+
when: jenkins_url is defined
10+
11+
- name: Set jenkins_url fact for HTTP
12+
set_fact:
13+
jenkins_url: "http://{{ jenkins_hostname }}:{{ jenkins_port }}"
14+
when: jenkins_port != "-1" and jenkins_url is not defined
15+
16+
# Note that this task will overwrite jenkins_url if both HTTP and HTTPS ports are defined.
17+
# This is by intention.
18+
- name: Set jenkins_url fact for HTTPS
19+
set_fact:
20+
jenkins_url: "https://{{ jenkins_hostname }}:{{ jenkins_port_https }}"
21+
when: jenkins_port_https != "-1" and jenkins_url is not defined
22+
423
- include: "{{ jenkins_install_via }}/install.yml"
524

25+
- include: "configure-config.yml"
26+
when: jenkins_install_via != "docker"
27+
628
- include: "configure-jenkins.yml"
729

830
- include: "configure-files.yml"

0 commit comments

Comments
 (0)