Skip to main content

installing jenkins with terraform and ansible in DigitalOcean droplet

Jenkins is an open-source project written in Java that runs on Windows, macOS, and other Unix-like operating systems. The first point of attraction in Jenkins is that it is open source. Apart from that, it is community-supported and primarily deployed on-premises, but it can also run-on cloud servers. Its integrations with Docker and Kubernetes take advantage of containers to roll out even more frequent releases.

In our project, we will use terraform to create our server on digital ocean and we will do the configuration and installation with Ansible.

Prerequisites

  1. Terraform already install 
  2. Ansible already install
  3. A digital ocean account
Deploying the virtual machine

We will use a 4 Gib virtual machine host on digital ocean, since we are going to use terraform to connect to our account, we will need to create a token in digitalOcean by following the steps in this link https://docs.digitalocean.com/reference/api/create-personal-access-token/ .

Once you get your token, you can generate an RSA key with ssh-keygen. You just have to run the command :


ssh-keygen -t rsa


For security reasons, we are going to use a variable to store this token, we do not want any random person to get access to our environment, so it is good practice to store it in an environment variable.
We will call it DO_PAT, to do so, we run the command 

export DO_PAT="your_personal_access_token"

  
To use the DigitalOcean provider with Terraform, you have to tell Terraform about it and configure the plugin with the proper credential variables. Create a file called provider.tf, which will store the configuration for the provider:


terraform {
  required_providers {
    digitalocean = {
      source = "digitalocean/digitalocean"
      version = "~> 2.0"
    }
  }
}

variable "do_token" {}
variable "pvt_key" {}

provider "digitalocean" {
  token = var.do_token
}

data "digitalocean_ssh_key" "terraform" {
  name = "terraform"
}



you can create your file server.tf, this file will have the config for your server in digital ocean.



resource "digitalocean_droplet" "server-1" {
    image = "ubuntu-22-10-x64"
    name = "jenkins"
    region = "tor1"
    size = "s-2vcpu-4gb"
    ssh_keys = [
      data.digitalocean_ssh_key.terraform.id
    ]
}

Setting the TF_LOG environment variable to 1 will enable detailed logging of what Terraform is trying to do. You can set it by running:

 

export TF_LOG=DEBUG

Initialize Terraform for your project by running:

terraform init

 Run the terraform plan command to see the execution plan, or what Terraform will attempt to do to build the infrastructure you described. You will have to specify the values for your DigitalOcean Access Token and the path to your private key, as your configuration uses this information to access your Droplet to install Nginx. Run the following command to create a plan:

terraform plan \
  -var "do_token=${DO_PAT}" \
  -var "pvt_key=$HOME/.ssh/id_rsa"

run terraform apply command to execute the current plan:

terraform apply \
  -var "do_token=${DO_PAT}" \
  -var "pvt_key=$HOME/.ssh/id_rsa"

At the end of all this, you will have a droplet name Jenkins up and running in digitalOcean






Configure the Jenkins server.


below you have the playbook that will allow you to install your Jenkins server on ubuntu. Do not forget to create your inventory file, it is just a file with your server address. It tells ansible where are your servers 

Just run the command:  


ansible-playbook -i inventory playbook.yml

You should have a fully functional Jenkins server running on http://YOUR_SERVER_IP_ADDRESS:8080

Playbook.yml

---
- hosts: jenkins
  become: true
  become_user: root
  tasks:
    - name: Update apt repo and cache on all Debian/Ubuntu boxes
      apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

    - name: Upgrade all packages on servers
      apt: upgrade=dist force_apt_get=yes

    - name: Check if a reboot is needed on all servers
      register: reboot_required_file
      stat: path=/var/run/reboot-required get_md5=no

    - name: Reboot the box if kernel updated
      reboot:
        msg: "Reboot initiated by Ansible for kernel updates"
        connect_timeout: 5
        reboot_timeout: 300
        pre_reboot_delay: 0
        post_reboot_delay: 30
        test_command: uptime
      when: reboot_required_file.stat.exists

    - name: Installing Java using Ansible
      become: yes
      apt:
        name: "{{ packages }}"
        state: present
      vars:
        packages:
           - openjdk-11-jdk

    - name: add the repository key to the system
      ansible.builtin.shell: curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
      args:
        executable: /bin/bash

    - name: add the repository key to the system
      ansible.builtin.shell: echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]  https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
      args:
        executable: /bin/bash

    - name: add the repository key to the system
      ansible.builtin.shell: apt update -y
      args:
        executable: /bin/bash

    - name: install jenkins
      apt:
        name: jenkins
        state: latest

    - name: start jenkins
      service:
          name: jenkins
          enabled: true
          state: started

inventory


[jenkins]

YOUR_SERVER_IP_ADDRESS

Comments

Popular posts from this blog

FastAPI Instrumentalisation with prometheus and grafana Part1 [Counter]

welcome to this hands-on lab on API instrumentation using Prometheus and FastAPI! In the world of modern software development, real-time API monitoring is essential for understanding usage patterns, debugging issues, and ensuring optimal performance. In this lab, we’ll demonstrate how to enhance a FastAPI-based application with Prometheus metrics to monitor its behavior effectively. We’ve already set up the lab environment for you, complete with Grafana, Prometheus, and a PostgreSQL database. While FastAPI’s integration with databases is outside the scope of this lab, our focus will be entirely on instrumentation and monitoring. For those interested in exploring the database integration or testing , you can review the code in our repository: FastAPI Monitoring Repository . What You’ll Learn In this lab, we’ll walk you through: Setting up Prometheus metrics in a FastAPI application. Instrumenting API endpoints to track: Number of requests HTTP methods Request paths Using Grafana to vi...

Join Ubuntu 20.04 to Active Directory with SSSD and SSH Access

Join Ubuntu 20.04 to Active Directory with SSSD and SSH Access  Overview This guide walks you through joining an Ubuntu 20.04 machine to an Active Directory domain using SSSD, configuring PAM for AD user logins over SSH, and enabling automatic creation of home directories upon first login. We’ll also cover troubleshooting steps and verification commands. Environment Used Component Value Ubuntu Client       ubuntu-client.bazboutey.local Active Directory FQDN   bazboutey.local Realm (Kerberos)   BAZBOUTEY.LOCAL AD Admin Account   Administrator Step 1: Prerequisites and Package Installation 1.1 Update system and install required packages bash sudo apt update sudo apt install realmd sssd libnss-sss libpam-sss adcli \ samba-common-bin oddjob oddjob-mkhomedir packagekit \ libpam-modules openssh-server Step 2: Test DNS and Kerberos Configuration Ensure that the client can resolve the AD domain and discover services. 2.1 Test domain name resol...

Observability with grafana and prometheus (SSO configutation with active directory)

How to Set Up Grafana Single Sign-On (SSO) with Active Directory (AD) Grafana is a powerful tool for monitoring and visualizing data. Integrating it with Active Directory (AD) for Single Sign-On (SSO) can streamline access and enhance security. This tutorial will guide you through the process of configuring Grafana with AD for SSO. Prerequisites Active Directory Domain : Ensure you have an AD domain set up. Domain: bazboutey.local AD Server IP: 192.168.170.212 Users: grafana (for binding AD) user1 (to demonstrate SSO) we will end up with a pattern like this below Grafana Installed : Install Grafana on your server. Grafana Server IP: 192.168.179.185 Administrator Privileges : Access to modify AD settings and Grafana configurations. Step 1: Configure AD for LDAP Integration Create a Service Account in AD: Open Active Directory Users and Computers. Create a user (e.g., grafana ). Assign this user a strong password (e.g., Grafana 123$ ) and ensure it doesn’t expire. Gather Required AD D...