Ansible Role to Install Jenkins on any platform
In this blog configuring a Jenkins using the Ansible Automation tool and integrating Ansible and Jenkins configuring dynamically on any platform like RHEL8,RHEL7, Fedora, Ubuntu most of versions
Ansible
Ansible is the simplest way to automate apps and IT infrastructure. Application Deployment + Configuration Management + Continuous Delivery.
Jenkins
Jenkins an open-source automation server which enables developers around the world to reliably build, test, and deploy their software.
Prerequisites
Python2 and Python3 Installed
Ansible Installed
Refer this for more information about Ansible setup
Starting our practical by configuring few files
Configure ansible.cfg file
Type your own path which you want to give inventory and roles_path here I’m giving a current role directory.
[defaults]
inventory=/root/ip.txt
host_key_checking=false
roles_path= /wp5_jenkins/[privilege-escalation]
become=true
become_method=sudo
become_user=root
become_ask_pass=false
Setup Inventory file based on your requirements Dynamic or Static
Create a Role
ansible-galaxy init <ROLE_NAME>
Now go to the tasks folder and creates some files for a different distribution of Linux.
First, we have gone a create main code to run tasks inside the main.yml file
# tasks file for jenkins
- name: using RHEL file
include_tasks: rhel.yml
when: ansible_distribution == "RedHat"- name: using CENTOS file
include_tasks: centos.yml
when: ansible_distribution == "CentOS"- name: using UBUNTU file
include_tasks: ubuntu.yml
when: ansible_distribution == "Ubuntu"- name: using FEDORA file
include_tasks: fedora.yml
when: ansible_distribution == "Fedora"
In this file using distribution to run on any Linux OS’s
Creating rehl.yml, centos, fedora, and Ubuntu files in the tasks folder.
RHEL 8 and RHEL7
---
# tasks file for jenkins on RHEL
- name: add jenkins key
rpm_key:
state: present
key: "{{ jenkins_key_rpm }}"- name: Repos
debug:
msg: "{{ item.value.description }} {{ item.value.name }}, {{ item.value.url }}"
loop:
"{{ lookup('dict',repos) }}"
- name: "Set EPEL yum repository"
yum_repository:
name: "{{ item.value.name }}"
baseurl: "{{ item.value.url }}"
description: "{{ item.value.description }}"
loop:
"{{ lookup('dict',repos) }}"- name: "Install Java and Jenkins"
yum:
name: "{{ item.value.name }}"
loop:
"{{ lookup('dict',softwares) }}"- name: "Starting jenkins"
service:
name: "jenkins"
state: started
enabled: yes
UBUNTU
---
# tasks file for jenkins
- name: add jenkins key
apt_key:
state: present
url: "{{ jenkins_key_rpmU }}"- name: "Set EPEL yum repository"
apt_repository:
filename: "{{ item.value.name }}"
repo: "{{ item.value.url }}"
description: "{{ item.value.description }}"
loop:
"{{ lookup('dict',reposU) }}"- name: "Install Java and Jenkins"
apt:
name: "{{ item.value.name }}"
state: present
loop:
"{{ lookup('dict',softwaresU) }}"- name: "Starting jenkins"
service:
name: "jenkins"
state: started
enabled: yes
CENTOS
---
# tasks file for jenkins on CENTOS
- name: add jenkins key
rpm_key:
state: present
key: "{{ jenkins_key_rpm }}"- name: Repos
debug:
msg: "{{ item.value.description }} {{ item.value.name }}, {{ item.value.url }}"
loop:
"{{ lookup('dict',repos) }}"
- name: "Set EPEL yum repository"
yum_repository:
name: "{{ item.value.name }}"
baseurl: "{{ item.value.url }}"
description: "{{ item.value.description }}"
loop:
"{{ lookup('dict',repos) }}"- name: "Install Java and Jenkins"
yum:
name: "{{ item.value.name }}"
state: present
loop:
"{{ lookup('dict',softwares) }}"- name: "Starting jenkins"
service:
name: "jenkins"
state: started
enabled: yes
FEDORA
---
# tasks file for jenkins on FEDORA
- name: add jenkins key
rpm_key:
state: present
key: "{{ jenkins_key_rpm }}"- name: Repos
debug:
msg: "{{ item.value.description }} {{ item.value.name }}, {{ item.value.url }}"
loop:
"{{ lookup('dict',repos) }}"
- name: "Set EPEL yum repository"
yum_repository:
name: "{{ item.value.name }}"
baseurl: "{{ item.value.url }}"
description: "{{ item.value.description }}"
loop:
"{{ lookup('dict',repos) }}"- name: "Install Java and Jenkins"
yum:
name: "{{ item.value.name }}"
state: present
loop:
"{{ lookup('dict',softwares) }}"- name: "Starting jenkins"
service:
name: "jenkins"
state: started
enabled: yes
Except UBUNTU I used yum_repository for adding repo of epel and Jenkins, for importing key and using rpm_key, and for installing the Jenkins used yum. Basically, Redhat, Centos OS, Fedora are similar. But for using lineinfile for adding the Jenkins repo URL in the sources.list file, apt for installation of java, apt_key for importing the Jenkins key, and after that adding the Jenkins
Creating vars
Go to /vars/main.yml file write this code
---
# vars file for jenkins
java: "java-1.8.0"
javaU: "openjdk-8-jre-headless"
reposU:
jenkins:
name: "Jenkins"
url: "https://pkg.jenkins.io/debian-stable/"
description: "Jenkins from Yum repo"repos:
epel:
name: "EPEL"
url: "https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm"
description: "EPEL release Yum repo"
jenkins:
name: "Jenkins"
url: "https://pkg.jenkins.io/redhat-stable/"
description: "Jenkins from Yum repo"softwaresU:
java:
name: "{{ javaU }}"
jenkins:
name: "jenkins"softwares:
java:
name: "{{ java }}"
jenkins:
name: "jenkins"
jenkins_key_rpmU: "https://pkg.jenkins.io/debian-stable/jenkins.io.key"
jenkins_key_rpm: "https://pkg.jenkins.io/redhat-stable/jenkins.io.key"
Here declared a variable on requirements of distribution Linux OS’s
The last setup creating a playbook to run a role
- hosts: os
become: true
roles:
- role: jenkins
Give a role name in the place of Jenkins
Run a role by executing the below command
>> ansible-playbook setup.yml
Supported OS’s are
- Redhat8
- Redhat7
- Ubuntu-20.04
- Ubuntu-18.04
- Ubuntu-16.04
- Centos8
- Centos7
- Fedora
GitHub Link
If you found this article helpful, please don’t forget to hit the Follow and Clap buttons to help me write more articles like this.
Thank You 🖤