Automation of NGINX installation using ansible

Automation of NGINX installation using ansible

Last Updated on February 18, 2023 by cscontents

Introduction

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.

In this article we will walk through the automation of NGINX installation. We will use ansible to automate the process. We will see the installation on a Red Hat Linux machine.

Prerequisite Knowledge

  • 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.

Introduction to Ansible | High Level Understanding of Ansible

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.

NGINX installation on Red Hat Linux and Ubuntu – step by step guide

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 –

Option 1:

If the change in this configuration file is needed in few lines then we can go with “lineinfile” module.

Option 2:

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

inventory.txt file

remote_server ansible_host=20.177.78.90 ansible_ssh_user=test_user ansible_ssh_pass=Pa$$w0rd

You need to put your target/remote machine details in the inventory.txt file.

nginx-install.yml 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”).

OR

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.

 

Thanks.

 

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.