Module 2 · Project 02 · Automated Server Configuration with Ansible · Lesson 7 of 48
Playbooks: users, packages, SSH and a firewall
Write a YAML playbook that installs packages, creates users, hardens SSH, installs Nginx and opens the firewall.
In this lesson · about 30 minutes
- Read and write the structure of a playbook: plays, hosts, become, tasks
- Choose the right module for each job
- Run a playbook and read its recap
The shape of a playbook
A playbook is a YAML file. YAML uses indentation (spaces, never tabs) to show what belongs to what, and a dash - for each item in a list.
# site.yml
- name: Baseline for every server # a play
hosts: all # which inventory group
become: true # run tasks with sudo
tasks:
- name: Install base packages
ansible.builtin.apt:
name: [curl, git, ufw, fail2ban]
state: present
update_cache: true
- name: Create the deploy user
ansible.builtin.user:
name: deploy
groups: sudo
shell: /bin/bash
- name: Add deploy's SSH public key
ansible.posix.authorized_key:
user: deploy
key: "{{ lookup('file', 'files/deploy.pub') }}"
- name: Turn off SSH password login
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: '^#?PasswordAuthentication'
line: PasswordAuthentication no
- name: Web servers
hosts: web
become: true
tasks:
- name: Install nginx
ansible.builtin.apt: { name: nginx, state: present }
- name: Allow SSH and HTTP through the firewall
community.general.ufw: { rule: allow, port: "{{ item }}", proto: tcp }
loop: ["22", "80", "443"]
- name: Turn the firewall on
community.general.ufw: { state: enabled }
- name: Start nginx now and on every boot
ansible.builtin.service: { name: nginx, state: started, enabled: true }
Read it from the top: a play picks hosts, and its tasks run in order. Every task has a name (shown in the output, so make it a clear sentence) and one module. loop repeats a task for each item, with the current one in {{ item }}.
Notice that the tasks describe results (state: present, state: started), not commands. That is what makes Ansible safe to re-run.
Whiteboard · watch and try
Anatomy of a task
Press Play to take one task apart.
Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.
Sorting drill
Pick the module
Which Ansible module would you use for each job?
- Make sure git is installed
- Create a deploy account in the sudo group
- Put our nginx.conf on the server
- Start nginx and enable it at boot
- Allow port 443
- Remove the telnet package
- Lock the old intern account
0 of 7 sorted
Hands-on lab (simulated terminal)
Lab: run the baseline playbook
Three fresh servers. Check the playbook, do a dry run, then apply it.
Step 1 of 4. Type each command and press Enter. Nothing runs on your computer.
1. Check the playbook's syntax.
Check your understanding0 of 3 correct
Key terms
Say the meaning to yourself, then tap a card to check.
Quiz score: 0 of 3
Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.