Operations 5 min read

Deploying Nginx with Ansible: Step‑by‑Step Role Configuration and Execution

This guide demonstrates how to automate the installation, configuration, and service management of Nginx across multiple hosts using Ansible roles, detailing environment setup, directory structure, task definitions, and execution commands.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Deploying Nginx with Ansible: Step‑by‑Step Role Configuration and Execution

The article provides a practical walkthrough for deploying Nginx with Ansible, targeting three hosts (192.168.236.182, 192.168.236.180, 192.168.236.181) where the latter two require Nginx installation.

It begins by showing the essential Ansible configuration files ( ansible.cfg , hosts , and the playbook nginx.yml ) and then displays the complete role directory tree, which includes files , handlers , meta , tasks , templates , and vars sub‑directories.

The main task file ( tasks/main.yml ) simply includes a series of specialized task files: install_deploy.yml , group.yml , user.yml , tar_nginx.yml , nginx_install.yml , service.yml , nginx_config.yml , and nginx_path.yml .

install_deploy.yml installs required packages (pcre, pcre-devel, openssl, openssl-devel, zlib, zlib-devel, gcc‑c++) using the yum module with a with_items loop.

group.yml creates the system group nginx , and user.yml creates a non‑login user nginx belonging to that group.

tar_nginx.yml extracts the Nginx source tarball ( nginx-1.20.2.tar.gz ) in /usr/local/src , while nginx_install.yml runs the typical ./configure , make , and make install steps to build Nginx under /opt/nginx .

service.yml copies a pre‑written nginx.service file to /usr/lib/systemd/system/ and triggers a systemctl reload notification.

nginx_config.yml uses the template module to render nginx.conf.j2 to /opt/nginx/conf/nginx.conf , creates a sub‑directory for additional configs, and notifies a service restart.

nginx_path.yml ensures the ownership of /opt/nginx is set to the nginx user and group.

Finally, the role is executed with ansible-playbook nginx.yml , and the article includes screenshots of the playbook run and a subsequent test to verify the Nginx service is up and running.

automationdeploymentDevOpsnginxAnsibleRole
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.