Configure Jenkins using Ansible on Amazon Linux 2023
Tags: jenkins, ansible, ec2, cicd
Published: 2024-02-24 12:29:06


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sngkj673rjkfp1hccuxk.png)
## Overview

Jenkins is a powerful automation server that can be used for continuous integration and continuous delivery (CI/CD) pipelines. Ansible is a popular configuration management tool that allows you to automate the provisioning and configuration of infrastructure. In this article, we'll explore how to use Ansible to configure Jenkins on Amazon Linux in the year 2023.

**Prerequisites**
Before we begin, make sure you have basic knowledge of Ansible and Jenkins and the following prerequisites:

- An AWS account
- An Amazon Linux instance up and running. (Amazon Linux 2023)
- SSH access to your Amazon Linux instance.

**Note**: Make sure to open the necessary ports (e.g., port 8080 for Jenkins) in the security group settings.

**Step 2: Install Ansible**

Connect into the ec2 instance either using ssh or session manager the run:
`sudo yum update && yum install ansible`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81jrnqvrgaq7cyvwtw53.png)

Check ansible version

`ansible --version
`

We will install Jenkins using Ansible playbook.

```
cat > setup_jenkins.yml<< EOF 
---
- hosts: localhost
  connection: local
  vars:
    java_packages:
      - java-17-amazon-corretto-devel
    jenkins_packages:
      - jenkins  
  tasks:
    - name: Download Jenkins repository file
      get_url:
        url: https://pkg.jenkins.io/redhat-stable/jenkins.repo
        dest: /etc/yum.repos.d/jenkins.repo
      become: true

    - name: Import Jenkins-CI key
      shell: rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
      become: true

    - name: Install Java
      yum:
        name: "{{ java_packages }}"
        state: present
      become: true

    - name: Install Jenkins
      yum:
        name: "{{ jenkins_packages }}"
        state: present
      become: true

    - name: Start Jenkins service
      service:
        name: jenkins
        state: started
      become: true
EOF
```
Then run:
`ansible-playbook setup_jenkins.yml
`
On success you should be able to see below output

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rg26ax7xwlft7163gij2.png)


Now go to your browser at http://<instance_IP>:8080/
You should find your password as shown:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8voo8t42alznbiwwccpy.png)

Get the password:
` sudo cat /var/lib/jenkins/secrets/initialAdminPassword
`
Unlock Jenkins: Follow the on-screen instructions to unlock Jenkins and complete the initial setup.
Configure Jenkins: Customize Jenkins settings and install plugins as needed to suit your requirements.

**Conclusion**
By following these steps, you have successfully configured Jenkins using Ansible on an Amazon Linux 2023 instance. You can now use Jenkins to automate your CI/CD workflows and streamline your development processes.
If you are experiencing an issue contact me on [LinkedIn](https://www.linkedin.com/in/maureen-chebet-b744b2102/)