0Pricing
Ansible Academy · Lesson

Desired State, Not Step-by-Step Scripts

Why declarative beats imperative.

Desired State, Not Step-by-Step Scripts is a free Ansible Academy lesson on CoddyKit — lesson 1 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.

Describe the End, Not the Steps

Ansible is declarative: you describe the end state you want, and Ansible figures out the steps to get there from wherever the host is now.

The Imperative Way

An imperative script lists exact commands in order. It assumes the starting state, so it breaks if the host is already partly set up.

apt-get update
apt-get install -y nginx
systemctl start nginx

The Declarative Way

A declarative task just states the goal: nginx should be installed and running. Ansible checks reality and acts only if needed.

ansible.builtin.package:
  name: nginx
  state: present

state Names the Goal

The state key is where you declare intent. present, started, or absent each name a desired end state, not an action to perform.

ansible.builtin.service:
  name: nginx
  state: started

Ansible Compares First

Before changing anything, a module compares the current state to the one you declared. If they already match, it does nothing at all.

This Is Convergence

Each run moves the host toward your declared state. This is convergence: no matter the starting point, the host ends in the same place.

Order Matters Less

Because tasks declare goals, re-running a half-finished play just finishes the rest. You do not have to track which step failed last time.

Safe to Run Again

Declarative tasks are safe to re-run. Already-correct hosts stay untouched, which is the whole foundation of idempotency.

Less to Remember

You stop writing 'if installed, skip' checks by hand. The module owns that logic, so your playbook stays short and readable.

A Playbook Is a Source of Truth

A declarative playbook reads like documentation of the desired server: what packages, services, and files should exist on it.

- name: Web server desired state
  hosts: web
  tasks:
    - ansible.builtin.package:
        name: nginx
        state: present

Some Tasks Are Still Imperative

Raw command and shell tasks are imperative: they run every time. Prefer state-based modules so Ansible can stay declarative.

Quick Check

You write a task saying nginx should be present, then run the playbook on a host that already has nginx.

Recap

You declare a desired state and Ansible converges the host toward it, acting only when reality differs. That safety is idempotency. 🎯

Frequently asked questions

Is the “Desired State, Not Step-by-Step Scripts” lesson free?

Yes — the full text of “Desired State, Not Step-by-Step Scripts” 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 “Desired State, Not Step-by-Step Scripts”?

Why declarative beats imperative. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Desired State, Not Step-by-Step Scripts” 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

  1. Desired State, Not Step-by-Step Scripts
  2. Reading changed vs ok in Output
  3. Why command Breaks Idempotency
  4. Check Mode: Dry-Run with --check
← Back to Ansible Academy