The Complete Magazine on Open Source

Ansible deployment of Jenkins

10.38K 0

DevOps, formed by combining the words ‘development’ and ‘operations’, is a software development process. Its area of focus is the communication and collaboration between product management, software development and operations professionals. This article is the sixth in the series.

In this article, we will install Jenkins using Ansible and set up a continuous integration (CI) build for a project that uses Git. Jenkins is free and open source automation server software that is used to build, deploy and automate projects. It is written in Java and released under the MIT licence. There are a number of plugins available to integrate Jenkins with other tools such as version control systems, APIs and databases.

Setting it up

A CentOS 6.8 virtual machine (VM) running on KVM will be used for the installation. Internet access should be available from the guest machine. The Ansible version used on the host (Parabola GNU/Linux-libre x86_64) is The ansible/ folder contains the following files:




The IP address of the guest CentOS 6.8 VM is added to the inventory file as shown below:

jenkins ansible_host= ansible_connection=ssh ansible_user=root ansible_password=password

An entry for the Jenkins host is also added to the /etc/hosts file as indicated below: jenkins

Figure 1: Unlocking Jenkins

Figure 2: Customising Jenkins

Figure 3: Getting started


The playbook to install the Jenkins server on the CentOS VM is given below:


- name: Install Jenkins software

hosts: jenkins

gather_facts: true

become: yes

become_method: sudo

tags: [jenkins]


- name: Update the software package repository


name: ‘*’

update_cache: yes

- name: Install dependencies


name: “{{ item }}”

state: latest


- java-1.8.0-openjdk

- git

- texlive-latex

- wget

- name: Download jenkins repo

command: wget -O /etc/yum.repos.d/jenkins.repo

- name: Import Jenkins CI key



state: present

- name: Install Jenkins


name: “{{ item }}”

state: latest


- jenkins

- name: Allow port 8080

shell: iptables -I INPUT -p tcp --dport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT

- name: Start the server


name: jenkins

state: started

- wait_for:

port: 8080

The playbook first updates the Yum repository and installs the Java OpenJDK software dependency required for Jenkins. The Git and Tex Live LaTeX packages are required to build our project We then download the Jenkins repository file and import the repository GPG key. The Jenkins server is then installed, port 8080 is allowed through the firewall, and the script waits for the server to listen on port 8080. The above playbook can be invoked using the following command:

$ ansible-playbook -i inventory/kvm/inventory playbooks/configuration/jenkins.yml -vv

Figure 4: Creating the first admin user

Figure 5: Jenkins is ready

Figure 6: The Jenkins dashboard


You can now open in the browser on the host to start configuring Jenkins. The Web page will prompt you to enter the initial administrator password from /var/lib/jenkins/secrets/initialAdminPassword to proceed further. This is shown in Figure 1.

The second step is to install the plugins. For this demonstration, you can select the ‘Install suggested plugins’ option, and later install any of the plugins that you require. Figure 2 displays the selected option.

After you select the ‘Install suggested plugins’ option, the plugins will get installed as shown in Figure 3.

An admin user is required for managing Jenkins. After installing the plugins, a form is displayed for you to enter the user name, password, name and e-mail address of the administrator. A screenshot of this is shown in Figure 4.

Once the administrator credentials are stored, a ‘Jenkins is ready!’ page will be displayed, as depicted in Figure 5.

You can now click on the ‘Start using Jenkins’ button to open the default Jenkins dashboard shown in Figure 6.

Figure 7: Enter an item name

Figure 8: Add the GitHub repo

An example of a new project

Let’s now create a new build for the project. Provide a name in the ‘Enter an item name’ text box and select the ‘Freestyle project’. Figure 7 shows the screenshot for creating a new project.

The next step is to add the GitHub repo to the Repositories section. The GitHub HTTPS URL is provided as we are not going to use any credentials in this example. By default, the master branch will be built. The form to enter the GitHub URL is shown in Figure 8.

A Makefile is available in the project source code, and hence we can simply invoke make to build the project. The Execute shell option is chosen in the Build step, and the make clean; make command is added to the Build step, as shown in Figure 9.

From the left panel, you can click on the Build Now link for the project to trigger a build. After a successful build, you should see a screenshot similar to Figure 10.

Figure 9: The Build step

Figure 10: Build success


An uninstall script to remove the Jenkins server is available in the playbooks/admin folder. It is given below for reference:



- name: Uninstall Jenkins

hosts: jenkins

gather_facts: true

become: yes

become_method: sudo

tags: [remove]


- name: Stop Jenkins server


name: jenkins

state: stopped

- name: Uninstall packages


name: “{{ item }}”

state: absent


- jenkins

The script can be invoked as follows:

$ ansible-playbook -i inventory/kvm/inventory playbooks/admin/uninstall-jenkins.yml


Your email address will not be published.