How to Install Nginx Using Ansible

Baldev yadav
Cypik
Published in
5 min readApr 11, 2024
nginx install using Ansible

Nginx is a popular open-source web server known for its high performance, reliability, and scalability. Ansible, on the other hand, is a powerful automation tool that enables you to configure and manage infrastructure as code. In this tutorial, we will guide you through the process of installing Nginx using Ansible Playbook.

Prerequisites

Before we get into the installation process, you need to make sure the following requirements are met:

  • A remote host running a supported Linux distribution (such as Ubuntu or CentOS)
  • SSH passwordless access to remote host
  • Ansible is installed on your local machine

step:

Step 1: Create Ansible Inventory

We will create a file inventory.cfglisting all the hosts we want to manage.

[cypik]
20.115.72.178 = Env=Demo

The above inventory file defines a single host under the "Web" group. The IP address of the host is 34.100.185.153, and Ansible will use the "ubuntu" user to connect to it.

This inventory file can be used by Ansible to run commands or playbooks on the specified host.

Run the below command to ping all hosts.

ansible -i inventory.cfg -m ping all

Output:

Step 2: Create an Ansible Playbook

We will create an Ansible playbook named Nginx. yaml to install the Nginx server on all the hosts mentioned under the web group.inventory.cfg

---
- hosts: cypik
remote_user: azureuser
become: yes
gather_facts: yes # Include gathering facts
tasks:
- name: Gather facts
setup:
- name: install nginx
apt:
name: nginx
state: latest
- name: start nginx
service:
name: nginx
state: started

The "Host" line specifies the group of hosts against which the playbook will run, in this case, the "Web" group.

The "Tasks" section lists the individual tasks that will be executed on each host in the group. In this case, there are two tasks.

The first task, "install nginx", uses the "apt" module to install the Nginx web server. The "name" parameter specifies the name of the package to install (in this case, "nginx"), and the "state" parameter specifies when Ansible should ensure that the package is installed and up to date.

The second function, "start nginx", uses the "service" module to start the nginx service. The "name" parameter specifies the name of the service (in this case, "nginx"), and the "state" parameter specifies when Ansible should ensure that the service is running.

Overall, this playbook will ensure that the Nginx web server is installed and running on all hosts in the "Web" group.

Step 3: Run Ansible Playbook

inventory.cfgWe have prepared our playbook install_nginx.yaml.

Execute the Ansible command below to run the playbook.

ansible-playbook nginx.yaml
  • ansible-playbookAnsible commands are used to run a playbook.
  • -i inventory.cfgSpecify the inventory file to use. In this case, the inventory file inventory.cfg.
  • nginx.ymlThe playbook is the name of the file to play. In this case, the playbook is the file nginx.yml.

So, putting it all together, the command nginx.ymlwill run the playbook against the hosts listed in the file, with elevated privileges.inventory.cfg

install nginx

Now we have successfully installed the Nginx server using the Ansible playbook.

Step 4: Uninstall Nginx using Ansible Playbook

We have learned how to install Nginx using the Ansible playbook, now we will see how we can uninstall Nginx using the Ansible playbook.

We will use our existing file inventory.cfgand for uninstall we will create a new yaml file name uninstall-nginx.yaml.

---
- hosts: cypik
remote_user: azureuser
become: yes
tasks:
- name: Gather facts
setup:
- name: stop nginx
service:
name: nginx
state: stopped

- name: uninstall nginx using apt-get
shell:
cmd: |
sudo apt-get remove nginx nginx-common -y &&
sudo apt-get purge nginx nginx-common -y &&
sudo apt-get autoremove -y
ignore_errors: yes
  • ---There is a YAML syntax marker at the beginning of the playbook indicating the beginning of the YAML file.
  • hosts: Web specifies that the playbook should apply to all hosts within the web in the inventory.
  • This taskssection lists the individual tasks to be performed on each host.
  • stop nginxThe first task uses the module to stop the Nginx service. serviceThe parameter namespecifies the name of the service to be stopped (in this case, "nginx"), and the parameter statespecifying how long Ansible should ensure that the service is stopped.
  • uninstall nginxThe second function uses the module to uninstall Nginx packages. aptThe parameter namespecifies the name of the package to uninstall (in this case, "nginx"), and statethe parameter specifies whether Ansible should ensure that the package is uninstalled.

Overall, this playbook will stop the Nginx service on all hosts and uninstall the Nginx package from each host.

Run the below command to uninstall nginx

ansible-playbook nginx.yaml

Output:

uninstall nginx

conclusion

Installing Nginx using Ansible is a straightforward and efficient way to manage large-scale web servers. Ansible makes it easy to automate the process of installing and configuring Nginx on multiple hosts, ensuring consistency and reliability across your infrastructure. We can use Ansible to configure Nginx by simply copying the Nginx conf file and reloading Nginx to use the new conf. We will cover the configuration part in the next blog.

Summary

In this post, I showed "How to Install Nginx using Ansible"

Enjoy it! That’s it; We did it…

For hassle-free cloud management with DevOps at the center of the process, contact us at info@cypik.com Cypik.

About the author:
I’m Baldev Yadav, an experienced Linux enthusiast and DevOps engineer. I'm passionate about automating and streamlining development processes, and currently, I work as a DevOps Engineer at Cypik. I specialize in cloud technologies with a focus on Ansible and DigitalOcean.

50

--

--