Look — I’ve been there. Late Friday night. Again. Me, staring at three Ubuntu servers, manually editing Nginx configs over SSH because “it’s just a quick change.” Spoiler: it never is. I remember the time I spent two hours debugging why one server wasn’t serving static assets — turned out I’d typoed the root path in /etc/nginx/sites-available/default. That typo went live at 10 PM. And my manager? Walked in Monday morning, sipped his chai, and said, “You’re still doing this by hand?” Yeah, I learned this the hard way.
📑 Table of Contents
- 🚀 Prerequisites — What You Need
- ⚙️ Installing Ansible on Ubuntu
- 🔧 Configuring Ansible Hosts
- 📦 Creating a Playbook to Install Nginx
- 📁 Preparing Files and Running the Playbook
- 🧠 Best Practices for Reliable Nginx Deployment
- 🔐 Use Variables and Templates
- 🔄 Idempotency — Your Best Friend
- 🧪 Test in Stages
- 🟩 Final Thoughts
- ❓ Frequently Asked Questions
- Can I use Ansible to install Nginx on multiple Ubuntu versions?
- Do I need to install Ansible on the target Ubuntu servers?
- How do I handle Nginx configuration reloads without downtime?
That was the day I finally gave Ansible a real shot.
Honestly, if you're running Ubuntu servers and deploying Nginx , you need Ansible like you need a working kettle in the office pantry. Not just for speed — but for peace of mind. No more “but it worked on my machine.” No more Sunday redeploy scrambles because web3 forgot the config override.
Let me walk you through how to ansible install nginx ubuntu properly. Step by step. So you don’t have to lose sleep over something that should be automated.
🚀 Prerequisites — What You Need
Before we dive into installation — here's the thing: tools don’t fix bad setups. I learned this during a migration at a 12-person startup in Bangalore. Team was sharp. Tools were solid. But no SSH keys properly rotated. We spent days untangling auth chaos.
- Control node : Your laptop or jump box. Linux preferred. Ubuntu/Debian? Even better. Windows users — use WSL2. Ansible doesn’t run natively on Windows, and wrestling with Cygwin ain’t worth it. (like this one — small, wry, self-aware)
- Managed nodes : Your Ubuntu boxes. Python 3 installed. SSH access open.
- SSH key-based auth. Non-negotiable. Trust me, password prompts mid-playbook destroy your flow. And your dignity.
- Sudo rights on target machines. Because Nginx needs root, and you’re not running everything as root. Right?
And yeah — skip one of these, and you’ll spend more time debugging than coding.
It’ll bite you. It always does.
⚙️ Installing Ansible on Ubuntu
Alright. Let’s get Ansible on your control machine. This is the brain. The puppet master. The one sending commands across your fleet.
Start simple:
sudo apt update
Get the latest package list. Don’t skip this. Old indices break installs.
Now install:
sudo apt install ansible -y
Done? Good. Now verify:
ansible --version
You’re looking for something like this:
ansible [core 2.14.3]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/youruser/.ansible/plugins/modules', ...]
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.10.6 (...)
If that shows up — congrats. You’ve just avoided another weekend in crisis mode.
First real win toward ansible install nginx ubuntu at scale.
🔧 Configuring Ansible Hosts
Ansible won’t magically know where your servers live. Gotta tell it.
Open the default inventory:
sudo nano /etc/ansible/hosts
Add your nodes. Group them. Makes life easier:
[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
[webservers:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa
Save. Exit.
Now test:
ansible webservers -m ping
If you get back pong — you’re golden.
If not? 9 times out of 10 — it’s SSH keys.
I once spent two hours debugging playbook syntax — only to realize I’d copied the public key to web3’s authorized_keys but not web2. Chai break. Realized my mistake. Fixed it in 30 seconds.
So yeah. Check your keys.
📦 Creating a Playbook to Install Nginx
This is where the magic happens.
Writing a playbook means no more manual steps. No more “I think I ran that command on all servers.”
Create your project:
mkdir nginx-deploy && cd nginx-deploy
Make the playbook:
nano site.yml
Now paste this in:
---
- name: Install and configure Nginx on Ubuntu
hosts: webservers
become: yes
tasks:
- name: Update apt cache
apt:
update_cache: yes
cache_valid_time: 3600
- name: Install Nginx
apt:
name: nginx
state: present
- name: Ensure Nginx is running and enabled
systemd:
name: nginx
state: started
enabled: yes
- name: Copy custom Nginx config
copy:
src: files/nginx.conf
dest: /etc/nginx/nginx.conf
owner: root
group: root
mode: '0644'
notify: Restart Nginx
handlers:
- name: Restart Nginx
systemd:
name: nginx
state: restarted
Let’s break it down — quick:
- become: yes — runs tasks with sudo.
- apt module — Ubuntu’s best friend.
- systemd — keeps Nginx alive after reboots.
- copy task — swaps in your config, if you’ve got one.
- Handlers — only run when triggered. Like a restart after config changes. Elegant.
"Automation isn't about replacing humans — it's about freeing them from the tasks that make them hate Fridays."
📁 Preparing Files and Running the Playbook
Want a custom nginx.conf? Create the folder:
mkdir files
nano files/nginx.conf
Start with the default. Tweak the server name. Add a reverse proxy. Whatever.
Then run it:
ansible-playbook site.yml
Watch the output.
Green OK lines? Good.
Green CHANGED? Normal — first run. (More onPythonTPoint tutorials)
No red FAILED? That’s the sound of a dev breathing easy.
Quick tip — use --check for dry runs:
ansible-playbook site.yml --check
Shows what would change. No actual changes. Safe for Fridays. Or when you’ve had three coffees and aren’t sure.
🧠 Best Practices for Reliable Nginx Deployment
Okay. You can ansible install nginx ubuntu. But can you do it well?
From what I’ve seen on real projects — it’s not about knowing Ansible. It’s about using it right. (Also read: 🐧 How to add user to sudo group ubuntu — key tips & common pitfalls)
🔐 Use Variables and Templates
Hardcoded values? That way lies madness.
A junior I was mentoring once pushed a playbook with server_name prod-api.somedomain.com — on a dev server. Spun up Nginx, broke local SSL, took down staging for 10 minutes. Oops.
So use templates. Jinja2. Variables.
Make a dir: (Also read: 🐧 AWS EC2 SSH permission denied fix Ubuntu — common mistakes and how to resolve them)
mkdir templates
nano templates/nginx.conf.j2
Add this:
worker_processes {{ ansible_processor_vcpus }};
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name {{ server_hostname }};
location / {
root /var/www/html;
index index.html;
}
}
}
Then update your task:
- name: Deploy templated Nginx config
template:
src: templates/nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart Nginx
Define server_hostname in vars or inventory. Done. Now it’s reusable.
🔄 Idempotency — Your Best Friend
Run the playbook. Run it again.
If it changes something the second time — you’ve got a problem.
Idempotency means: same result every time. No surprises.
The apt and systemd modules? Designed for this. They check state before acting.
Always test this. I can’t stress it enough.
🧪 Test in Stages
- Start with one server — not all five.
- Use
ansible-playbook --stepto go task-by-task. Pause. Verify. -
Add tags — makes debugging easier:
- name: Install Nginx apt: name: nginx state: present tags: install
Then run just that part:
ansible-playbook site.yml --tags install
Control. Clarity. Calm.
🟩 Final Thoughts
I remember the first time I ran a playbook across ten servers — finished in 90 seconds. All green. No errors. No missing steps.
No coffee spills. No frantic SSH checks.
Just consistency.
That’s when it clicked: this isn’t about Nginx. Or Ansible. It’s about system design. About shifting from firefighter to architect.
You're not just learning how to ansible install nginx ubuntu. You’re building muscle for scalable, maintainable infrastructure.
Whether you're a junior pulling late nights or a senior managing clusters — automation like this turns chaos into craft.
❓ Frequently Asked Questions
Can I use Ansible to install Nginx on multiple Ubuntu versions?
Yes. Ansible doesn’t care if it’s 18.04, 20.04, or 22.04 — as long as Python 3 and SSH are there. The apt module works across versions. Just test for compatibility, and use idempotent tasks. From what I’ve seen on real projects, it’s rock solid.
Do I need to install Ansible on the target Ubuntu servers?
Nope. That’s the beauty of it. Ansible is agentless. Runs from the control node. Target servers only need Python and SSH. Makes adoption smooth. Trust me, I’ve used Puppet before — this is so much lighter.

Top comments (0)