0Pricing
Ansible Academy · Lesson

Register a Result, Then Decide

Use registered output in conditions.

Register a Result, Then Decide 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.

React to What Happened

Often the next step depends on the last one. The register keyword saves a task's result so later tasks can read it. 📥

Capturing Output

Add register to store the result in a variable of your choosing. Here the command output lands in result.

- name: Check disk
  ansible.builtin.command: df -h /
  register: result

What Is Inside

A registered variable is rich. It holds rc (return code), stdout, stderr, and flags like changed and failed.

Decide on Return Code

Run a follow-up only when the previous command succeeded by checking its rc is zero.

when: result.rc == 0

Branch on changed

The changed flag tells you if the task altered the system. Trigger cleanup only when something actually changed.

when: result.changed

Search the Output

The in test scans stdout for text. This runs the next task only if the word active appeared.

when: "active" in result.stdout

Let It Fail, Then Handle

Pair register with ignore_errors so a failing task does not stop the play, letting you inspect and react afterward.

- ansible.builtin.command: /opt/check
  register: result
  ignore_errors: true

Check failed Yourself

After ignoring errors, test the failed flag to decide your own recovery path instead of crashing the play.

when: result.failed

Registered in a Loop

If the task uses a loop, the registered variable holds a results list, one entry per iteration, that you can examine.

Debug to Explore

Unsure of the shape? Print the whole variable with debug to see every key before you write your condition.

- ansible.builtin.debug:
    var: result

Capture, Then Decide

register turns a one-shot task into useful state. Capture the result, then let when steer what runs next. 🎯

Quick Check

Let us confirm how register and when work together.

Recap

Use register to save a task result, then read rc, changed, stdout or failed in a later when. ✅

Frequently asked questions

Is the “Register a Result, Then Decide” lesson free?

Yes — the full text of “Register a Result, Then Decide” 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 “Register a Result, Then Decide”?

Use registered output in conditions. 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 “Register a Result, Then Decide” 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. Run a Task Only when True
  2. Branch on OS Family Facts
  3. Combining Conditions with and / or
  4. Register a Result, Then Decide
← Back to Ansible Academy