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

Jenkins

Prerequisites

Python2 and Python3 Installed

Ansible Installed

Configure ansible.cfg file

[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
ansible.cfg

Setup Inventory file based on your requirements Dynamic or Static

Create a Role

ansible-galaxy init <ROLE_NAME> 
creating a role
main tasks
# 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"
/tasks/main.yml

Creating rehl.yml, centos, fedora, and Ubuntu files in the tasks folder.

---
# 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
/tasks/rhel.yml
---
# 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
/tasks/ubuntu.yml
---
# 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
/tasks/centos.yml
---
# 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
/tasks/fedora.yml

Creating vars

---
# 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"
/vars/main.yml

The last setup creating a playbook to run a role

- hosts: os
become: true
roles:
- role: jenkins
/setup.yml

Run a role by executing the below command

>> ansible-playbook setup.yml
  • Redhat8
  • Redhat7
  • Ubuntu-20.04
  • Ubuntu-18.04
  • Ubuntu-16.04
  • Centos8
  • Centos7
  • Fedora

GitHub Link

Keep Learning !! Keep Sharing !!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vinodha kumara

DevSecOps, MLOps, Cloud Computing, DE, ML, DL, Programmer, Blogger, Volunteer