Module 2 · Project 02 · Automated Server Configuration with Ansible · Lesson 9 of 48
Idempotency, dry runs and environments
Why a good playbook is safe to run a hundred times, how to preview changes, and one playbook for dev, staging and production.
In this lesson · about 25 minutes
- Define idempotency and spot non-idempotent tasks
- Preview changes with --check and --diff
- Structure inventories and variables for dev, staging and production
Idempotent: same result, however many times
An operation is idempotent if doing it twice leaves things the same as doing it once. Pressing a lift's call button is idempotent; appending a line to a file is not.
That property is what lets you run a playbook every hour to undo drift, or re-run it after a half-failed attempt, without fear. On a server that already matches, a good playbook reports changed=0.
Most modules are idempotent by design. The danger is command and shell, which just run whatever you give them every time. When you must use them:
- add
creates: /pathso the task is skipped if the file it makes already exists, - or set
changed_when:so Ansible reports honestly whether anything changed, - or better, find a real module that does the job.
Try it · watch it run
Run it twice: script vs. playbook
Run a plain setup script and the equivalent playbook on the same fresh server. Then run them again, and again.
A plain Bash script
set -e echo 'PasswordAuthentication no' \ >> /etc/ssh/sshd_config useradd -m deploy apt-get install -y nginx
(not run yet)
tail /etc/ssh/sshd_config
(no setting yet)
An Ansible playbook
- lineinfile:
path: /etc/ssh/sshd_config
line: PasswordAuthentication no
- user: name=deploy
- apt: name=nginx state=present(not run yet)
tail /etc/ssh/sshd_config
(no setting yet)
Look before you leap: check mode
ansible-playbook site.yml --check --diff is a dry run: Ansible works out what it would change and shows the line-by-line differences in files, without changing anything. Run it before touching production, and in a scheduled job to detect drift (someone changed a server by hand).
One playbook, many environments
Real teams have at least dev, staging and production. They should be configured by the same playbook, so what you tested in staging is what reaches production. Only the inventory and variables differ:
server-config/
├── site.yml
├── roles/ …
├── templates/
└── inventories/
├── dev/
│ ├── hosts.ini
│ └── group_vars/web.yml
├── staging/
│ ├── hosts.ini
│ └── group_vars/web.yml
└── production/
├── hosts.ini
└── group_vars/web.yml
Then ansible-playbook -i inventories/staging site.yml configures staging and -i inventories/production configures production. As playbooks grow, teams split them into roles (a folder per job, such as nginx/ or users/, each with its own tasks, handlers and templates) so they can be reused.
Try it · watch it run
One playbook, three environments
Switch environments and watch the same template render different config files.
inventories/dev/hosts.ini
[web] dev-web-01
inventories/dev/group_vars/web.yml
server_name: dev.noviq.test worker_processes: 1 app_port: 3000 log_level: debug enable_tls: false
templates/nginx.conf.j2 (the same for all)
worker_processes {{ worker_processes }};server {listen {{ 443 if enable_tls else 80 }};server_name {{ server_name }};error_log /var/log/nginx/error.log {{ log_level }};location / {proxy_pass http://127.0.0.1:{{ app_port }};}}
Rendered on 1 dev server
worker_processes 1;
server {
listen 80;
server_name dev.noviq.test;
error_log /var/log/nginx/error.log debug;
location / {
proxy_pass http://127.0.0.1:3000;
}
}Whiteboard · watch and try
Spot the non-idempotent task
Press Play. The tutor reviews three tasks from a teammate's playbook.
Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.
Check your understanding0 of 4 correct
Key terms
Say the meaning to yourself, then tap a card to check.
Quiz score: 0 of 4
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.