Automating Nginx Deployment with Jenkins and Ansible
Introduction
In this article, we will explore how to automate the deployment of Nginx using Jenkins and Ansible. We will create an experimental environment using Docker Compose and Vagrant, and then configure Jenkins to automatically deploy Nginx whenever the configuration is pushed to GitHub. This process is idempotent, meaning that it will produce the same result regardless of how many times the code is changed.
Experimental Environment
To build our experimental environment, we will use Docker Compose and Vagrant. We will start by creating a virtual machine using Vagrant, which will serve as our target machine for deploying Nginx. We will then use Docker Compose to build our Jenkins and Jenkins agent images.
Starting the Experimental Environment
To start our experimental environment, we will run the following command:
git clone https://github.com/zacker330/jenkins-ansible-nginx.git
cd jenkins-ansible-nginx
Next, we will build our Jenkins agent image using the following command:
docker build -f JenkinsSlaveAnsibleDockerfile -t jenkins-swarm-ansible
We will then start our Jenkins agent swarm client using the following command:
docker-compose up -d
Finally, we will access our Jenkins master at http://localhost:8080 and follow the prompts to install the necessary plugins, including the Pipeline 2.6 and Swarm 3.15 plugins.
Configuring Jenkins
To configure Jenkins, we will set the security configuration to open ports for the Jenkins agent connection. We will also set the Jenkins master to open port 50,000, which can be changed to a random port if desired.
Creating a Deployment Task on Jenkins
To create a deployment task on Jenkins, we will create a new pipeline configuration that pulls tasks from a remote repository. We will use the Jenkinsfile to define the pipeline logic, which will be discussed in more detail later.
Manually Triggering a Build Automation
To manually trigger a build automation, we will click the “Build Now” button on the Jenkins dashboard. This will execute the pipeline and deploy Nginx to our target machine.
Code Explanation
The pipeline logic is defined in the Jenkinsfile, which is shown below:
pipeline {
agent {
label 'ansible'
}
environment {
ANSIBLE_HOST_KEY_CHECKING = false
}
triggers {
pollSCM('H/1 * * * *')
}
stages {
stage('deploy nginx') {
steps {
sh 'ansible-playbook -i env-conf/dev deploy/playbook.yaml'
}
}
}
}
This pipeline defines a single stage called “deploy nginx”, which executes an Ansible playbook to deploy Nginx to our target machine.
Deployment Logic
The deployment logic is defined in the deploy/playbook.yaml file, which is shown below:
---
hosts: "nginx"
become: true
roles:
- ansible-role-nginx
- ansible-role-firewall
This playbook defines a role called ansible-role-nginx, which installs and configures Nginx on our target machine. It also defines a role called ansible-role-firewall, which sets up the server firewall rules.
Configuration Management
The configuration management is handled by Ansible, which separates the environment configuration from the script execution. This allows us to easily manage and version our configuration files.
Conclusion
In this article, we have explored how to automate the deployment of Nginx using Jenkins and Ansible. We have created an experimental environment using Docker Compose and Vagrant, and then configured Jenkins to automatically deploy Nginx whenever the configuration is pushed to GitHub. This process is idempotent, meaning that it will produce the same result regardless of how many times the code is changed.
Appendix
The experimental environment code can be found on GitHub at https://github.com/zacker330/jenkins-ansible-nginx.