Deploying Nginx with Ansible: Complete Playbook and Configuration Guide
This tutorial walks through using Ansible to automate the installation, configuration, and startup of Nginx across multiple Linux servers, detailing the required environment, role directory layout, task definitions, and verification steps with example commands and code snippets.
The article presents a practical Ansible playbook for deploying Nginx on three hosts (192.168.236.182, 192.168.236.180, 192.168.236.181), showing how to automate package installation, user/group creation, source extraction, compilation, and service startup.
Environment : The control node (ansible) resides at 192.168.236.182, while the target nodes for Nginx installation are 192.168.236.180 and 192.168.236.181.
Role directory structure (displayed with tree -L 4) includes folders such as default, files (containing nginx-1.20.2.tar.gz and nginx.service), handlers, meta, tasks, and templates (with index.html.j2 and nginx.conf.j2).
Main task file (tasks/main.yml) defines the following steps (code snippets shown in ... blocks):
- name: copy nginx
copy: src=nginx-1.20.2.tar.gz dest=/usr/local/src/ - name: create group nginx
group: name=nginx state=present system=yes - name: create user nginx
user: name=nginx system=yes shell=/sbin/nologin group=nginx - name: yum pcre pcre-devel openssl openssl-devel zlib zlib-devel gcc-c++
yum: name={{ item }} state=present
with_items:
- pcre
- pcre-devel
- openssl
- openssl-devel
- zlib
- zlib-devel
- gcc-c++ - name: unarchive nginx
unarchive: src=/usr/local/src/nginx-1.20.2.tar.gz dest=/usr/local/src/ - name: install nginx
shell: chdir=/usr/local/src/nginx-1.20.2 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module && make && make install - name: copy nginx.conf
template: src=nginx.conf.j2 dest=/usr/local/nginx/conf/nginx.conf - name: nginx start
shell: /usr/local/nginx/sbin/nginxTo run the playbook, execute ansible-playbook nginx.yml from the control node, as illustrated by the screenshots included in the original article.
After execution, the article shows how to verify that Nginx is running on the target machines, again using screenshots of the status check.
Overall, the guide provides a reproducible method for automating Nginx deployment with Ansible, suitable for DevOps and operations teams.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
