Install Nginx in Five Lines
Write a first useful, real playbook.
Install Nginx in Five Lines is a free Ansible Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Real, Useful Playbook
Time to build something real. You will write a tiny playbook that installs the nginx web server on your hosts.
Start the Play
Every playbook begins with a dash and the hosts line that names the group you want to configure.
---
- hosts: webYou Need Root
Installing packages needs root, so add become. It tells Ansible to escalate privileges with sudo for this play.
- hosts: web
become: trueOpen the tasks List
Under the play, the tasks key starts the list of work. Each item below it is one thing Ansible will do.
tasks:Name Your Task
Give the task a clear name. It shows up in the output so you know exactly which step is running.
- name: Install nginxUse the apt Module
On Debian and Ubuntu, the apt module manages packages. You hand it a package name and the state you want.
apt:
name: nginx
state: presentstate present Means Installed
Setting state: present means make sure nginx is installed. If it already is, Ansible leaves it untouched.
The Whole Playbook
Put it together and you have a complete, working playbook in just a handful of lines. That is the power of Ansible.
---
- hosts: web
become: true
tasks:
- name: Install nginx
apt:
name: nginx
state: presentRun It
Save it as nginx.yml and launch it with ansible-playbook. Watch the recap to confirm nginx was installed.
ansible-playbook -i inventory.ini nginx.ymlMake Sure It Is Running
Add a second task with the service module to start nginx, so the server is not just installed but actually serving.
- name: Start nginx
service:
name: nginx
state: startedRun It Twice, Safely
Run the playbook again and the tasks report ok, not changed. Ansible already reached the state, so it does nothing twice.
Quick Check
Your playbook installs a package. Why add become to the play?
Your First Real Deploy
You wrote and ran a genuine playbook that installs and starts nginx. That is config management in action. 🎉
Frequently asked questions
Is the “Install Nginx in Five Lines” lesson free?
Yes — the full text of “Install Nginx in Five Lines” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Install Nginx in Five Lines”?
Write a first useful, real playbook. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Install Nginx in Five Lines” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- YAML Syntax You Need for Ansible
- From Ad-Hoc to a Repeatable Playbook
- Run It with ansible-playbook
- Install Nginx in Five Lines