Deploying Nginx with Ansible Playbooks
This article explains how Ansible playbooks—YAML‑based, flexible configuration management files—can be used to automate the installation, configuration, and service management of Nginx across multiple hosts, including preparation of servers, playbook structure, execution commands, and verification steps.
Playbooks Overview Ansible playbooks are a flexible, YAML‑based configuration management and multi‑host deployment system that allows ordered execution of tasks, variables, templates, handlers, and roles, supporting both synchronous and asynchronous operations.
Core Elements Tasks (list of operations), Variables, Templates, Handlers (triggered actions), Roles.
Deploying Nginx with a Playbook The example sets up two servers (192.168.20.41 as the Ansible control node and 192.168.20.42 as the target), distributes nginx_static.conf , and defines nginx_install.yaml with hosts, variables, tasks to install Nginx, copy the template, notify a handler to restart, and ensure the service is running.
---
- hosts: webservers
vars:
http_port: 8080
server_name: www.hahashen.com
remote_user: root
gather_facts: false
tasks:
- name: 安装nginx
yum: pkg=nginx state=latest
- name: 写入nginx配置文件
template: src=nginx_static.conf dest=/etc/nginx/nginx_static.conf
notify:
- restart nginx
- name: 确保nginx正在运行
service: name=nginx state=started enabled=yes
handlers:
- name: restart nginx
service: name=nginx state=reloadedRunning ansible-playbook -C nginx_install.yaml checks the playbook syntax, and ansible-playbook nginx_install.yaml distributes the configuration and starts the service, as shown by the subsequent command‑line and screenshot outputs.
After deployment, verify the Nginx configuration with nginx -t , which should report "syntax is ok" and a successful test.
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.