Install Jenkins using Ansible Playbook

Install Jenkins using Ansible Playbook

 

What is Jenkins & Ansible?

Jenkins is one of the most powerful and commonly used Continuous Integration and Deployment (CI/CD) tool in the DevOps world. A Job in Jenkins denotes an automated flow created to do a particular set of tasks. There are different types of Jobs available in the Jenkins like Freestyle, Pipeline, DSL, etc. with the most common being the Jenkins Pipeline job.

Ansible is one of the most powerful and commonly used open-source Configuration management tool in the DevOps world. It is widely used for software provisioning, automate apps, configuration management and application deployment. It is compatible to run on almost all platforms.

Today's Agenda

In this post, we will learn how to install Jenkins using Ansible Playbook. We will do this installation using the Normal Jenkins Installation with default user token generated and default plugins installed. Refer to this link install Jenkins using the Customised Installation (i.e. with Custom User Credentials and Plugins).

Prerequisite

This post has been prepared for the audience who : 
  1. Have access to a system with Ansible installed and configured on it.
  2. Have a very basic understanding of Linux commands to debug if there is any errors.
  3. Have a server setup on which Jenkins needs to be installed.
  4. And finally, are eager to learn and try such powerful tools.

Let's get started

 The Normal Way (With Default User & Plugins) 

Step 1: Create an empty Ansible playbook structure for Jenkins.

Create the main work directory in which you will run the below steps.

$ mkdir -p ansible/roles/

$ cd ansible/

$ touch Jenkins.yml ansible.cfg


Run the below command to create a default directory structure that we can change further.
 

$ cd roles/

$ ansible-galaxy init jenkins


Output of the above command
Output of the above command

Now, we will make changes in the following 2 files:
 
    1.  ansible > roles > jenkins > defaults > main.yml
    2.  ansible > roles > jenkins > tasks > main.yml


Step 2: Change required playbook files for Jenkins.

In the Ansible playbook file structure, tasks > main.yml file include the main installation steps.

---
#  tasks file for Jenkins

- name: Install packages and dependencies
apt: name={{ item }} update_cache=yes state=present
with_items:
      - openjdk-8-jdk  # this specific version is needed for Jenkins to run
      - libfontconfig
      - libffi-dev
      - libncurses5-dev

- name: Add the Jenkins public GPG key to the apt repo
apt_key: url=http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key
                      state=present

- name: Add Jenkins apt repo to the trusted sources list
apt_repository: repo='deb http://pkg.jenkins-ci.org/debian binary/'
                                  
update_cache=yes
                                   state=present

- name: Install Jenkins server
apt: name=jenkins state=present

- name: install python3-pip
apt:
     pkg:
          - python3-pip

- name: Install additional Python packages
pip: name={{ item }} state=latest
with_items:
      - ansible
      - boto
      - flake8
      - passlib

- name: Start jenkins server
  systemd:
      name: jenkins
      enabled: yes
      state: started

- name: download jenkins cli jar file
get_url:
      url: "http://localhost:8080/jnlpJars/jenkins-cli.jar"
      dest: "{{ jenkins_home }}/jenkins-cli.jar"
      mode: '0664'

- name: Restart jenkins server
systemd:
      name: jenkins
      state: restarted


In the Ansible playbook file structure, defaults > main.yml file include the variables that we want to use in our main installation script (tasks > main.yml) file.
 

---
# defaults file for Jenkins

jenkins_home: /var/lib/jenkins
jenkins_hostname: localhost
jenkins_http_port: 8080



Step 3: Provision file to add Jenkins server details.

Now we make changes in the files that will help Ansible to know the server details on which Jenkins needs to be installed.
 
    1.  ansible > jenkins.yml
    2.  sensible > ansible.cfg

In Ansible, jenkins.yml file includes the hosts, users, basic variables, roles, etc that playbook will need while running.

---

- name: Provision Jenkins CI on server

   hosts: jenkins

   remote_user: ubuntu

   become: yes

   roles:

       - jenkins


   vars:

       - update_apt_cache: yes


In Ansible playbook file structure, ansible.cfg file includes the host's authentication details like IP, pem path, etc.
 
Replace the <jenkins_private_ip> and <pem_path> with the actual values.
 

[jenkins]

<jenkins_private_ip>   ansible_ssh_private_key_file=<pem_path>


Step 4: Run the playbook to get the results.

Run the below command and wait for it to execute successfully.
 

$ ansible-playbook -i ansible.cfg jenkins.yml








Comments