NGINX is one of the most popular open source web server, and it is free to use. Not only web server, NGINX can be used as reverse proxy, load balancer etc.
- Hands-on experience in Ansible
- Knowledge of NGINX
If you want to learn Ansible basics then please check out the below course from KodeKloud which offers one of the best learning material in DevOps world.
Link of Training Course: Ansible for absolute beginners
Note: The above links are affiliate links, if you enroll this course using the above link, then it would help us to get some monetary benefit from KodeKloud. It won’t cost you anything.
Prerequisite Server setup
Since we will automate the installation using ansible, so we will need two machines.
- Ansible host – it is the machine where ansible will be installed.
- Target/remote machine – it is the target or remote machine where ansible will perform the task.
To get high level information about working model of ansible, you can follow below article.
And you will need a non-root user with sudo privileges on the target/remote machine, if you are performing this installation on a test machine for experimental purpose you can go with “root” user.
Prerequisite manual steps
Before we automate any task, we need to have manual steps. In this case, we need the manual steps to install NGINX.
In the below article we have discussed manual installation of NGINX step by step. You can have a look at the steps and then if you come back here it will be lot easier.
Automation of nginx installation
Below are the details you need to know about automated installation of nginx using ansible.
Ansible modules used in the playbook
Before we go to the ansible playbook, it will be better if you have basic knowledge about the below ansible modules which will be used in our playbook.
One very important point, when we install nginx we get nginx configuration file (/etc/nginx/nginx.conf). Now based on the requirement you may edit this file. There is two option –
If the change in this configuration file is needed in few lines then we can go with “lineinfile” module.
If the change in the configuration file is need in many files and need to add few more extra lines, in that case we can pass the edited/modified/customized configuration file with ansible playbook and ansible will replace the default configuration file with your customized configuration file. For this we can use “copy” module.
We will go with Option 2 which is more flexible.
Playbook & Inventory file
We have 2 files. Below are details.
- nginx-install.yml : This is ansible playbook where we kept the tasks. You can name the file as you wish.
- inventory.txt : This is the file where we kept the details of target/remote machine
remote_server ansible_host=22.214.171.124 ansible_ssh_user=test_user ansible_ssh_pass=Pa$$w0rd
You need to put your target/remote machine details in the inventory.txt file.
- name: NGINX Installation hosts: remote_server become: true tasks: - name: Enable nginx 1.16 module command: yum -y module enable nginx:1.16 - name: Ensure nginx is installed package: name: nginx state: present - name: Replace nginx configuration file copy: src: './nginx.conf' dest: '/etc/nginx/' owner: root group: root mode: 0644 - name: start nginx service: name: nginx state: started
Now we need to run the below command on Ansible host,
ansible-playbook playbook.yml -i inventory.txt --ask-become-pass -vv
The above command will ask for password which is needed to run any command on target/remote machine with sudo privileges (since in the playbook we are using “become: true”).
We can pass the sudo password with the command.
ansible-playbook playbook.yml -i inventory.txt --extra-vars "ansible_sudo_pass=Pa$$w0rd" -vv
This will initiate the installation of nginx on the target/remote machine.
If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.
- Kubernetes Series: Part 1 – Introduction to Kubernetes | Background of Kubernetes
- Kubernetes Series: Part 2 – Components of Kubernetes cluster | Kubernetes cluster in detail
- Kubernetes Series: Part 3 – What is Minikube and How to create a Kubernetes cluster (on Linux) using Minikube?
- Introduction to Ansible | High Level Understanding of Ansible
- Basics of automation using Ansible | Automate any task
- Automation of Java installation – using Ansible
- Automation of JBoss EAP installation – using ansible
- Jenkins Pipeline as code – High level information
- Jenkins pipeline script to build Java application and push artifacts into repository
- Jenkins pipeline script to build & deploy application on web server
- What is End-to-End Monitoring of any web application, and Why do we need it?
- What is “Monitoring” in DevOps? Why do we need to Monitor App/DB servers, Transactions etc.?
- DevOps Engineer or Software Developer Engineer which is better for you?- Let’s discuss
- How To Be A Good DevOps Engineer?
- How to do git push, git pull, git add, git commit etc. with Bitbucket