How to install and configure Jenkins using Ansible Playbook

How to install and configure Jenkins using Ansible Playbook

ansible_playbook.jpg

Test Environment

Ansible Controller – Fedora 32 linux with ansible installed
Ansible Node – Fedora 32 linux remote node

In this article we will see how we can install and configure jenkins using the Ansible Playbook. For setting up a Jenkins service on a remote note we need the below minimum requirements that should be satisfied.

– Remote node with Fedora 32 installed
– Minimum RAM – 256 MB
– Mimimum HD – 1 GB

Once we have the remote node setup with the above requirements we can go ahead and execute our tasks for setting up the jenkins usiing the the playbook. Here are the high level details of the tasks that we will carry out as a part of this activity.

– JRE – Java 8 with 64 bit for run execution
– JDK/JRE – Java 8 or 11 for job execution
– Adding the Jenkins Repository
– Installing the Jenkins and Java development packages
– Start the Jenkins service
– Add jenkins service and port 8080 to allow from firewall

If you are interested in watching the video. Here is the youtube video on the same step by step procedure to setup Jenkins using playbook.

Procedure –

Step1: Ensure Jenkins repository is added

Create the below playbook to add the jenkins repository to install the packages and import the gpg key.

Ansible playbook to setup jenkins repository and install jenkis and its dependencies
[admin@fedser jenkinssetup]$ cat setup_jenkins.yaml 
---
- hosts: stack
  remote_user: admin
  become: true
  tasks:
    - name: Ensure Jenkins repository is added
      yum_repository:
        name: jenkins-ci
        description: jenkins-ci package repository
        baseurl: http://pkg.jenkins.io/redhat
        gpgkey: https://pkg.jenkins.io/redhat/jenkins.io.key
        gpgcheck: yes
    - name: Ensure gpp key is imported
      rpm_key:
        state: present
        key: https://pkg.jenkins.io/redhat/jenkins.io.key
    - name: Ensure Jenkins and java-devel package installed
      yum:
        name: '{{ packages }}'
        state: present
        update_cache: true
      vars:
        packages:
          - jenkins
          - java-devel

Execute the below playbook to add the jenkins repository.

Execute Ansible playbook to setup jenkins repository and install jenkins and its dependencies
[admin@fedser jenkinssetup]$ ansible-playbook setup_jenkins.yaml -K
BECOME password: 

PLAY [stack] *************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.47.130]

TASK [Ensure Jenkins repository is added] ********************************************************************************************************************
changed: [192.168.47.130]

TASK [Ensure gpp key is imported] ****************************************************************************************************************************
changed: [192.168.47.130]

TASK [Ensure Jenkins and java-devel package installed] *******************************************************************************************************
changed: [192.168.47.130]

PLAY RECAP ***************************************************************************************************************************************************
192.168.47.130             : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Validate the remote node whether the repository got added or not.

Validate Jenkins repository setup and package installation on remote node
[admin@ansinode yum.repos.d]$ cat jenkins-ci.repo 

[jenkins-ci]

baseurl = http://pkg.jenkins.io/redhat gpgcheck = 1 gpgkey = https://pkg.jenkins.io/redhat/jenkins.io.key name = jenkins-ci package repository [admin@ansinode yum.repos.d]$ rpm -qa | grep jenkins jenkins-2.286-1.1.noarch [admin@ansinode yum.repos.d]$ rpm -qa | grep java-1.8.0-openjdk-devel* java-1.8.0-openjdk-devel-1.8.0.282.b08-0.fc32.x86_64

Step2: Start and Enable the Jenkins service with firewall service and port enablement

Update Ansible Playbook to Start the Jenkins service and open firewall port and service
[admin@fedser jenkinssetup]$ cat setup_jenkins.yaml 
---
- hosts: stack
  remote_user: admin
  become: true
  tasks:
    - name: Ensure Jenkins repository is added
      yum_repository:
        name: jenkins-ci
        description: jenkins-ci package repository
        baseurl: http://pkg.jenkins.io/redhat
        gpgkey: https://pkg.jenkins.io/redhat/jenkins.io.key
        gpgcheck: yes
    - name: Ensure gpp key is imported
      rpm_key:
        state: present
        key: https://pkg.jenkins.io/redhat/jenkins.io.key
    - name: Ensure Jenkins and java-devel package installed
      yum:
        name: '{{ packages }}'
        state: present
        update_cache: true
      vars:
        packages:
          - jenkins
          - java-devel
    - name: Ensure systemd daemon reloaded
      command: systemctl daemon-reload
    - name: Ensure Jenkins service is enabled and started
      service:
        name: jenkins
        state: started
    - name: Ensure jenkins service added to firewalld
      firewalld:
        service: jenkins
        state: enabled
        permanent: yes
        immediate: yes
    - name: Ensure port 8080 added to firewalld
      firewalld:
        port: 8080/tcp
        state: enabled
        permanent: yes
        immediate: yes

Validate the remote node whether the firewall setup completed and service is running or not.

Validate the Firewall setup and Jenkins service status
[admin@ansinode ~]$ sudo firewall-cmd --list-all
[sudo] password for admin: 
FedoraServer (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: cockpit dhcpv6-client http https jenkins ssh
  ports: 8080/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

[admin@ansinode ~]$ systemctl status jenkins.service 
● jenkins.service - LSB: Jenkins Automation Server
     Loaded: loaded (/etc/rc.d/init.d/jenkins; generated)
     Active: active (running) since Sat 2021-04-10 07:27:11 IST; 1h 0min ago
       Docs: man:systemd-sysv-generator(8)
      Tasks: 35 (limit: 2292)
     Memory: 429.4M
        CPU: 55.586s
     CGroup: /system.slice/jenkins.service
             └─1906 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenki>
...

Step3: Validate the Jenkins service url

Access the Jenkins URL from remote network

Access the Jenkins URL
URL - http://192.168.47.130:8080/

First time when you access the Jenkins url, it will ask to enter the jenkins user default password available in a particular file and then setup the user with which you want to login to the Jenkins portal. Once the user setup is completed it will ask to install a set of plugins or customize the plugin list that you want to install. Once you select on of those options the plugins will be installed and your Jenkins server is ready for use.

Hope you enjoyed reading this article. Thank you..