In this blog, we have explained How to configure Jenkins with Nginx using Ansible Playbook easy step-by-step installation. There is a simple ansible structure for Jenkins with Nginx package installation configuration.
There are task and config folders, the nginx-jenkins.yml file, which is an ansible-playbook.


The hosts file is an inventory file in the task folder and the nginx-jenkins.yml is a playbook YAML file that installs and configure Jenkins with Nginx. There is a config folder, in this folder default.conf of Nginx configuration file, nginx.repo file for install the Nginx from Nginx repository and Jenkins.conf file for Jenkins configuration with Nginx.

Topic covers

  • Ansible playbook for Jenkins and Nginx installation, configuration.
  • Explain the modules and it’s parameters in the nginx-jenkins.yml.
  • Files in the config folder.
  • Execute the playbook.

Download the ansible-playbook from git:
# git clone https://github.com/smarttechfunda/ansible-playbook.git
Go to folder ansible-playbook/install-nginx-jenkins run the following commands:
# cd ansible-playbook/install-nginx-jenkins
# ansible-playbook -i hosts nginx-jenkins.yml

Note: The installation time depends upon the internet network connectivity.
OR
Follow the below manual method and understand how to write and run the ansible-playbook, which installs, configure Jenkins and Nginx.

Ansible playbook for Jenkins and Nginx installation, configuration:

Ansible Playbook Structure:

Run the following command to create the task and config folders:
# mkdir -p task/config
Run the following command to create the hosts and nginx-jenkins.yml (you can change the name of the nginx-jenkins.yml file to the main.yml file. In this example we are using the nginx-jenkins.yml name.) files in the task folder. Also, you can create a hosts file outside the task folder or inside the task folder.
# cd task
# touch hosts nginx-jenkins.yml

# cd task/config
# touch default.conf jenkins.conf nginx.repo
The above touch creates the hosts and nginx-jenkins.yml files under the task folder. Also, the touch command creates the default.conf, jenkins.conf and nginx.repo files in the path task/config.
Copy and paste the code to hosts, nginx-jenkins.yml, default.conf, jenkins.conf and nginx.repo files.


Note:
For default.conf, jenkins.conf and nginx.repo files check the point “Files in the config folder“. Also, run the following command on the remote host/IP to check the continuous installation log.
# tailf /var/log/messages
OR
You can write the code of hosts, nginx-jenkins.yml, default.conf, jenkins.conf and nginx.repo files. It will help you to understand the structure of the hosts and nginx-jenkins.yml files.

Explain the modules and it’s parameters in the nginx-jenkins.yml:

task:
The task is the folder, in which hosts and nginx-jenkins.yml files created.
The task name denotes the task or steps to perform.

hosts:
The hosts file in an inventory of hosts group. There two hosts files. For the individual playbook hosts file and another is on path /etc/ansible/hosts file which is a global file for all playbook. In this example, we are using the individual playbook hosts file. The hosts file contains the IP, hostname, and different groups of hosts.
Check the following formate of hosts and nginx-jenkins.yml files.

hosts file:

Check the following code of the hosts file in the task folder. We used the remote IP and jenkins as the group name of the hosts. If you use the hostname, then it should be resolvable by DNS, OR if you use IP, then it should be reachable and configured passwordless ssh for hostname OR IP in both cases. For passwordless ssh automate-the-installation-of-ansible-on-centos-8 Step 6.

[jenkins]
192.168.0.108
nginx-jenkins.yml:

main.yml or nginx-jenkins.yml:
The main.yml or nginx-jenkins.yml is a playbook, in which we can write the code or set of instructions to execute on any instance. This playbook installs Jenkins and Nginx packages.


Check the following nginx-jenkins.yml file code in the task folder:

---
# This playbook install jenkins and configured with nginx.

#
# The following statement means the all hosts in webserver
# and all hosts in jenkins. Check the hosts file in install-nginx-jenkins folder.
#
- hosts: jenkins

  tasks:
    #
    # To start the jenkin service after jenkins installation, we need to install the java.
    # The yum module install java.
    #
  - name: Java installation in process...
    yum:
      name: java
    register: java_status

    # The debug module print variable value of java_status.
  - name: Print the java installation status
    debug:
      msg: "Java installation status: {{java_status}}"

    #
    # The get_url is a module which download the provided URL to the destition (dest)
    # and assign the permission to the downloaded file.
    # The linux command is wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
    #
  - name: Downloading Jenkins Repo File...
    get_url:
      url: https://pkg.jenkins.io/redhat-stable/jenkins.repo
      dest: /etc/yum.repos.d/jenkins.repo
      mode: '0644'
    # The register module store status of get_url module.
    register: jenkins_repo

    # The debug module print variable value of jenkins_repo.
  - name: Print Download Jenkins Repo File Status
    debug:
      msg: "Downloaded the Jenkins repo file status: {{jenkins_repo}}"

    # Copying the nginx.repo file to the path /etc/yum.repos.d/.
  - name: Copying nginx.repo file to the path /etc/yum.repos.d/...
    copy:
      src: config/nginx.repo
      dest: /etc/yum.repos.d/
      mode: '0644'
    register: copy_nginx_repo

    # The debug module print variable value of copy_nginx_repo.
  - name: Copy nginx file
    debug:
      msg: "Copy nginx.repo file status: {{copy_nginx_repo}}"

    #
    # The rpm_key is a module, it import the key from the URL.
    # Linux command to import key from URL: rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
    #
  - name: Importing the key from a ULR...
    rpm_key:
      state: present
      key: https://pkg.jenkins.io/redhat-stable/jenkins.io.key
    # The register module store status of ansible.builtin.rpm_key module.
    register: jenkins_key

    # The debug module print variable value of jenkins_key.
  - name: Print Import Key Status
    debug:
      msg: "Imported the jenkins key: {{jenkins_key}}"

    #
    # The yum module install the provided package.
    # Linux command for to install jenkins: yum install -y jenkins nginx
    #
  - name: Jenkins and Nginx installation in process...
    yum:
      name: "{{ item }}"
    loop:
      - jenkins
      - nginx
    register: installation_status

   # The debug module print variable value of installation_status.
  - name: Print Jenkins and Nginx Installation Status
    debug:
      msg: "Jenkins installed: {{installation_status}}"

    # Replace listen 80 default_server; with listen 80; in  /etc/nginx/nginx.conf file.
  - name: Updating the /etc/nginx/nginx.conf file...
    shell: sed  -i "s/listen       80 default_server;/listen       80;/g" /etc/nginx/nginx.conf


   # Copy jenkins.conf and default.conf to the path /etc/nginx/conf.d/.
  - name: Copying jenkins.conf file with owner and permissions...
    copy:
      src: config/{{ item }}
      dest: /etc/nginx/conf.d/
      owner: root
      group: root
      mode: '0644'
    loop:
      - jenkins.conf
      - default.conf

    #
    # The service module enable and start the jenkins, nginx services.
    # Linux command to enable and start the jenkins and nginx:
    # systemctl enable jenkins && systemctl start jenkins
    # systemctl enable nginx && systemctl start nginx
    #
  - name: Enable and start the jenkins service...
    service:
      name: "{{ item }}"
      enabled: yes
      state: started
    loop:
      - jenkins
      - nginx
    register: service_start_status

    # The debug module print variable value of jenkins_enable_start_status.
  - name: Print the enable and start status of jenkins
    debug:
      msg: "Enable and start jenkins status: {{service_start_status}}"

    #
    # Enable the port 8080 for Jenkins and service http for nginx in iptables.
    # Enable the selinux for http.
    #
  - name: Enabling jenkins port in iptables...
    shell: "{{ item }}"
    loop:
        - firewall-cmd --permanent --zone=public --add-port=8080/tcp
        - firewall-cmd --permanent --zone=public --add-service=http
        - firewall-cmd --reload
        - setsebool -P httpd_can_network_connect 1

    # The debug module print variable value of ansible_ssh_host which remote host ip.
  - name: Display the Jenkins URL
    debug:
      msg: "Use this URL to configure Jenkins: http://{{ ansible_ssh_host }}"

hosts:
In the main.yml or nginx-jenkins.yml file, the hosts is a keyword and its value is IP or hostname or group of IPs. The hosts file is an inventory file of a group of hosts.

tasks:
The tasks which execute the modules like a copy, debug, rpm_key, yum, service, etc

name:
Lable of the task.

debug module:
The debug is a module to print messages during the execution for debugging the tasks. This module has a msg parameter key, which displays the messages. Also, a print variable value is declared in the register module.
In this example, we declare the following variables.

TaskVariable Name
Java installation{{java_status}}
jenkins.repo download{{jenkins_repo}}
nginx.repo copy{{copy_nginx_repo}}
Import jenkins.io.key {{jenkins_key}}
Jenkins and Nginx installation{{installation_status}}
Start Jenkins and Nginx services{{service_start_status}}
Display the remote host IP {{ansible_ssh_host}}
Task and it’s variable Name table

yum module:
The yum module installs the provided package. The package name should be matched with the OS package name.

yum module paramters:

name:
Provide the package name like Nginx, net-tools, etc to install on remote install.
state:
The state parameter key provides the kind of instructions like install or present or latest ( These options install the packages or install the latest packages.) and absent or remove (These options remove the packages.) packages of the name provided to the “name:” parameter key.

service module:

The service module reload, start, restart and stop the service after installation of service package.

service module paramters:

name:
Service names like Nginx, Jenkins.
enabled:
After installation service package needs to enable or not service after booting the instance. The values of the enabled parameter key are yes or no.
State:
After installation, the service package state can be reloaded or started or restarted or stopped.

register module:
The register is a module to store the variable value in the playbook. Use the debug module to print variable values stores in the variable.

get_url module:
The get_url module downloads the file from a remote server like http, ftp, etc.

get_url module parameters:

url:
The url path of remote file stage on http, ftp, etc servers.
dest:
The destination path where to download the file from a remote server.
mode:
The mode of the file, read, write and execute after download. The numerical formate is
4: read
2: write
1: execute

rpm_key module:
The rpm_key module imports the key file of any required package.

rpm_key module parameters:

key:
The URL path of the package key file.
state:
There are two choices present or absent. This will be imported(present) or removed(absent) the key from the rpm database.

shell:
It executes the command in nodes.

Copy module:
The copy module copy file or directory from source to destination. Also, change the owner and permission of the file or direcotry.

Copy module parameter:

src:
Source path of file or directory. Copy from the path.
dest:
Destination path of file or directory. Where to copy the file or directory.
owner:
Owner of the file or directory.
group:
Group of the file or directory.
mode:
Mode of the file or directory. The mode is read, write and execute.
Check the following numerical formate of the file or directory mode.
4: Read
2: Write
1: Execute

loop:
In this playbook, we have used a loop to avoid duplicate code structure. Check the following code. In this code, we installed the Jenkins and Nginx packages in the single yum module and do not need to write two yum modules for 2 package installations. The item is a variable that reads the list of items in the loop according to a sequence.

name: Jenkins and Nginx installation in process...
    yum:
      name: "{{ item }}"
    loop:
      - jenkins
      - nginx

Files in the config folder:

Copy the following files to the path install-nginx-jenkins/config/

default.conf:
server {
    listen       80;
    server_name  localhost;


    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }


    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
jenkins.conf:
upstream jenkins {
    server 127.0.0.1:8080;
}

server {
    listen      80;
    server_name localhost;

    access_log  /var/log/nginx/jenkins.access.log;
    error_log   /var/log/nginx/jenkins.error.log;

    proxy_buffers 16 64k;
    proxy_buffer_size 128k;

    location / {
        proxy_pass  http://127.0.0.1:8080;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;

        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto http;
    }

}
nginx.repo:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

Execute the playbook:

After creating the files and folders, go to the task folder and run the following command:
# cd task
# ansible-playbook -i hosts nginx-jenkins.yml

Check the following final output.
Ansible Playbook for Jenkins and Nignx

After configuration of Jenkins with Nginx using ansible-playbook, browse the following URL in your favorite browser. Also, check the following image to configure the Jenkins.
http://<IP> or http://<fqdn>
IP: This is IP of the remote host.
fqdn: The fqdn means fully qualified domain name. If you configured the DNS and added fqdn against IP in the DNS then use fqdn.

Jenkins Configuration