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

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...

Deploying a Scalable Monitoring Stack Lab on AWS using Terraform and Ansible

Deploying a Scalable Monitoring Stack Lab on AWS using Terraform and Ansible Introduction Effective monitoring is a cornerstone of cloud infrastructure management, ensuring high availability and performance. This guide provides a professional walkthrough on deploying Prometheus , Grafana , and Node Exporter on AWS using Terraform for infrastructure provisioning and Ansible for configuration management. This lab will create a prometheus server and a grafana server, It will install node exporter on both server. You should be able to see the metrics in grafana, we already install a node exporter dashboard for the user. The diagram below will give you an idea of what the architecture will look like If you want to replicate this lab, you can find the complete code repository here: GitHub - MireCloud Terraform Infra .  Infrastructure Setup with Terraform 1. Creating a Dedicated VPC To ensure isolation, we define a VPC named Monitoring with a CIDR block of 10.0.0.0/16 . reso...

Building a Static Website on AWS with Terraform

The Journey to a Fully Automated Website Deployment A few weeks ago, I found myself needing to deploy a simple static website . Manually setting up an S3 bucket, configuring permissions, and linking it to a CloudFront distribution seemed like a tedious process. As someone who loves automation, I decided to leverage Terraform to simplify the entire process. Why Terraform? Infrastructure as Code (IaC) is a game-changer. With Terraform, I could:  Avoid manual setup errors  Easily reproduce and  Automate security best practices Instead of clicking through AWS settings, I wrote a few Terraform scripts and deployed everything in minutes. Let me walk you through how I did it!  Architecture Overview The architecture consists of three main components: User:  The end user accesses the website via a CloudFront URL.  CloudFront Distribution:  Acts as a content delivery network (CDN) to distribute content efficiently, reduce latency, and enhance security. It ...