Install Jenkins to AWS EC2 Instance With Ansible

Sesudah create AWS EC2 Instance menggunakan Terraform, selanjut nya install App Jenkins menggunakan Ansible, Struktur folder Ansible akan seperti ini.

inventory

[ip-server]
18.143.101.**

[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/cehamot/BigprojectStudiDevops/mdrdani.pem

roles/update-OS/tasks/main.yaml

- name: update server linux Debian/Ubuntu
  shell: apt update
  when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'


- name: update server linux redhat/centos
  shell: apt update
  when: ansible_distribution == 'CentOs' or ansible_distribution == 'Red Hat Enterprise Linux'

roles/Jenkins/tasks/main.yaml

- name: Import Jenkins key from URL
    ansible.builtin.get_url:
      url: https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
      dest: /usr/share/keyrings/jenkins-keyring.asc

  - name: Dowload Long term jenkins release
    ansible.builtin.apt_repository:
      repo: "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/"
      state: present

  - name: Update Repository
    shell: apt update

  - name: Install Jenkins
    apt:
      name: jenkins
      state: latest

  - name: daemon-reload to pick up config changes
    ansible.builtin.systemd:
      daemon_reload: yes

  - name: start jenkins
    ansible.builtin.systemd:
      name: jenkins
      state: started

roles/java/tasks/main.yaml

- name: Install Java
    become: yes
    apt:
        name: "{{ packages }}"
        state: present
    vars:
        packages:
           - openjdk-11-jre

roles/docker/tasks/main.yaml

- name: Install depedencies
    apt:
      name: "{{item}}"
      state: present
      update_cache: yes
    loop:
      - apt-transport-https
      - ca-certificates
      - curl
      - gnupg-agent
      - software-properties-common

  - name: Docker Official GPG key
    apt_key:
      url: https://download.docker.com/linux/ubuntu/gpg
      state: present

  - name : Repository Docker
    apt_repository:
      repo: deb https://download.docker.com/linux/ubuntu focal stable
      state: present

  - name: Install Docker
    apt:
      name: "{{item}}"
      state: latest
      update_cache: yes
    loop:
      - docker-ce
      - docker-ce-cli
      - containerd.io

  - name: add user mod
    command: sudo usermod -aG docker jenkins

  - name: Make Sure docker is running
    service:
      name: docker
      state: started
      enabled: yes

server-jenkins-playbook.yaml

- name: Server Jenkins
  hosts: ip-server
  become: yes
  roles:
    - roles/update-os
    - roles/java
    - roles/jenkins
    - roles/docker

run

$ ansible-playbook -i inventory server-jenkins-playbook.yaml

Oke Jenkins sudah running, selanjutnya akses menggunakan browser ke IP Public instance. Jika belum bisa di akses seperti gambar di bawah ini maka kita harus setting security instance nya untuk allow port 8080.

Ke bagian Security Groups, Edit Inbound tambahkan seperti ini. Save rules

balik ke browser lagi akses menggunakan port 8080

untuk unlock jenkins kita harus membuka file /var/lib/jenkins/secrets/initialAdminPassword di instance.

copy kan password ke dalam kolom Administrator password tersebut dan continue.

Pilih saja install suggested plugins nanti akan di install plugin2 yang biasa nya di perlukan. Masukan username, password dan full name. Save and continue.

sampai di sini pastikan URL sama dengan yang link browser di atas nya. save and finish

Yey Start using jenkins !!