emmetog.jenkins
Ansible Role for Jenkins
This Ansible role installs and sets up Jenkins completely.
Use this role if you want to keep your Jenkins setup version-controlled. This way, you can deploy Jenkins consistently and treat it like infrastructure instead of a pet.
If you just need to install Jenkins and prefer to use the web interface for configuration without needing to repeat the deployment, then this role isn’t for you. You can check out geerlingguy/ansible-role-jenkins.
Requirements
For Docker deployment, you need Docker installed on your server. Currently, Docker, apt-get, and yum are the only supported methods, but more can be added.
Installation
To install, use Ansible Galaxy:
$ ansible-galaxy install emmetog.jenkins
Role Variables
Here are the important variables for installing Jenkins:
jenkins_install_via
: Specifies the installation method (must be one of the following):docker
: Install in a Docker containerapt
: Install on Ubuntu/Debianyum
: Install on RedHat/CentOS
jenkins_version
: The exact Jenkins version to install
And here are the variables for configuration:
jenkins_url
: The URL to access Jenkinsjenkins_port
: The port Jenkins listens onjenkins_home
: Directory for Jenkins configuration filesjenkins_admin
: Admin email for Jenkinsjenkins_java_opts
: Options for the Java processjenkins_config_owner
: Owner of configuration filesjenkins_config_group
: Group for configuration filesjenkins_auth
: How Ansible should authenticate with Jenkinsjenkins_url_health_check
: URL for checking Jenkins' health after startupjenkins_health_check_user
: Username for health check authenticationjenkins_health_check_password
: Password for health check authentication
To customize jobs and plugins:
jenkins_jobs
: List of job names to include in Jenkinsjenkins_plugins
: List of plugin IDs to installjenkins_custom_plugins
: List of custom plugins to install
For more variables, check defaults/main.yml
.
Example Playbook
- hosts: jenkins
vars:
jenkins_version: "2.73.1"
jenkins_hostname: "jenkins.example.com"
jenkins_port: 8080
jenkins_install_via: "docker"
jenkins_jobs:
- "my-first-job"
- "another-awesome-job"
jenkins_include_secrets: true
jenkins_include_custom_files: true
jenkins_custom_files:
- src: "jenkins.plugins.openstack.compute.UserDataConfig.xml"
dest: "jenkins.plugins.openstack.compute.UserDataConfig.xml"
jenkins_plugins:
- git
- blueocean
jenkins_custom_plugins:
- "openstack-cloud-plugin/openstack-cloud.jpi"
roles:
- emmetog.jenkins
Managing Configuration Files
For job configuration, the role looks for files in:
{{ playbook_dir }}/jenkins-configs/jobs/my-first-job/config.xml
and
{{ playbook_dir }}/jenkins-configs/jobs/another-awesome-job/config.xml
.
You can customize the directories using jenkins_source_dir_configs
and jenkins_source_dir_jobs
.
The role will also seek a config.xml
file in {{ playbook_dir }}/jenkins-configs/config.xml
to serve as the job configuration template.
If you update a configuration file or add new jobs/plugins, the workflow is:
- Change the configuration in the Jenkins UI.
- Save the resulting XML files to your version control system (VCS).
- Add new files to their respective lists:
- For new jobs:
jenkins_jobs
- For custom files:
jenkins_include_custom_files
- For custom plugins:
jenkins_custom_plugins
- For new jobs:
Example Jenkins Configuration File
In {{ jenkins_source_dir_configs }}/config.xml
, you can set up global Jenkins configuration, like this:
<?xml version='1.1' encoding='UTF-8'?>
<hudson>
...
</hudson>
Example Job Configuration File
For {{ playbook_dir }}/jenkins-configs/jobs/my-first-job/config.xml
, you could use:
<?xml version='1.0' encoding='UTF-8'?>
<project>
<description>My first job, it says "hello world"</description>
<builders>
<hudson.tasks.Shell>
<command>echo "Hello World!"</command>
</hudson.tasks.Shell>
</builders>
</project>
Authentication and Security
To secure Jenkins, you can use:
- API token-based authentication (recommended).
- Crumb-based authentication (for added CSRF protection).
- No security (not recommended).
API token-based authentication
This is recommended for easy revocation and tracking. Here’s how to create one:
- Create a special user or log in as an admin.
- In the user settings, add a new API token.
- Save the token value, preferably in an Ansible vault.
- Define necessary variables in your playbook.
Crumb-based authentication
To prevent CSRF attacks, set this up if API tokens aren't practical. Note that it works with the "Anyone can do anything" access control.
You’ll need to install the Strict Crumb Issuer plugin if using Jenkins 2.176.2+.
HTTPS
To enable HTTPS in Jenkins:
- Define
jenkins_port_https
for the HTTPS port. - Set up either JKS keystore or CA signed certificate details.
- Use a reverse proxy for Docker deployments for easier management.
License
MIT
Author Information
Created with care by Emmet O'Grady, founder of NimbleCI, which builds Docker containers for GitHub projects.
You can follow my insights on my personal blog and Docker topics on the NimbleCI blog.
Installs and completely configures Jenkins using Ansible
ansible-galaxy install emmetog.jenkins