top of page

Install Jenkins on CentOS Using Ansible

To install Jenkins on CentOS using Ansible, you can use the following playbook. Ensure you have Ansible installed and configured to connect to your target CentOS server.

---
- name: Install Jenkins on CentOS 7
  hosts: jenkinshost
  become: yes

  tasks:
    - name: Install Java (OpenJDK)
      yum:
        name: java-11-openjdk
        state: present

    - name: Add Jenkins YUM repository
      yum_repository:
        name: jenkins
        description: Jenkins
        baseurl: http://pkg.jenkins.io/redhat-stable
        gpgcheck: yes
        gpgkey: https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
        state: present

    - name: Install Jenkins
      yum:
        name: jenkins
        state: present

    - name: Start Jenkins service
      service:
        name: jenkins
        state: started
        enabled: yes

    - name: Get Jenkins unlock key
      command: cat /var/lib/jenkins/secrets/initialAdminPassword
      register: accesskey

    - name: Display Jenkins unlock key
      debug:
        var: accesskey.stdout

Here's what each task in the playbook does:

  1. Installs OpenJDK 11, which is required by Jenkins.

  2. Adds the Jenkins YUM repository for CentOS 7, which contains the Jenkins packages.

  3. Installs the Jenkins package.

  4. Starts the Jenkins service and enables it to start at boot.

  5. Retrieves the Jenkins unlock key, which you need for the initial setup.

  6. Displays the unlock key to the console so you can use it to complete the Jenkins setup through the web interface.

Here's the output of the above playbook


Ansible

Now, let's try to browse the Jenkins URL over port 8080.


Jenkins

As you can see here we are able to access Jenkins via browser using the server's IP address and port 8080. You'll need to enter the unlock key to set up Jenkins.

bottom of page