What Ansible's register directive does

300-435 By islever Editorial Last updated

Short answer

A The register directive captures the output of an Ansible task, such as a show command result, into a variable for later tasks to use

Question

What is Ansible 'register' directive?
  1. Captures task output into a variable for later use Correct Answer
  2. Registers device
  3. Creates user
  4. Logs event

Key Takeaways

Ansible's register directive captures the output of a task, such as a show command result, into a variable that subsequent tasks in the same play can read. Option A is correct because register stores task output for later use, not device enrolment, user creation, or event logging. The variable holds structured fields like stdout, stdout_lines, rc, and failed for conditional logic.

Expert Analysis

register stores output of a task (like show command result) for subsequent tasks.

The register directive in Ansible is a task-level keyword that captures the result of one task into a named variable so later tasks in the same play can read and act on it. In this question the only option that states this mechanism is A, which says it "Captures task output into a variable for later use." That matches the behaviour precisely: after a task runs, Ansible places stdout, stdout_lines, rc, failed, changed, msg and related fields into the variable named on the register line.

The other three options describe unrelated actions. B, "Registers device," misreads register as device enrolment or inventory registration, which in Ansible is done through inventory files or inventory plugins, not a task directive. C, "Creates user," describes a task such as ansible.builtin.user rather than what the directive itself does — a user task could be followed by register, but register never creates the account. D, "Logs event," is a side effect at best; while registering a result could later feed a logging task, register itself performs no logging action.

The mechanism this question relies on

What the question does not spell out is that register only makes the result available within the play it belongs to (and only to tasks that run after the registering task). The variable is scoped to the play, not globally to the whole playbook run, and each registered name shadows any prior variable of the same name. In a concrete case: a play that runs ansible.builtin.command: show version on a switch with register: ver, then uses {{ ver.stdout }} in a later debug or set_fact, works because the command task's output was captured; the same {{ ver.stdout }} would be undefined in a later, separate play. Registered results are also structured, not plain text — ver.stdout is the raw string, while ver.stdout_lines is a list, which matters when a later task loops over lines.

Two patterns commonly paired with register are worth noting because they confirm its purpose: failed_when / changed_when, which inspect the registered result to decide whether the task should be counted as a failure or a change, and when: conditionals, which branch on fields such as ver.failed or ver.rc!= 0. Both only work because register put the output into a variable in the first place.

For exam purposes, treat register as the standard way to persist task output for downstream logic. The distractors A–D here are simple action words, so the discriminator is whether the option names capturing output into a variable rather than performing an unrelated operation on the device or the system.

The correct choice is therefore A.

Exam Strategy

When a question names an Ansible keyword, identify what it does to the task result rather than what it does to the device. Read each option and discard any that describes an action on hardware or users; the correct answer will describe handling of the task's output. This pattern repeats across Ansible keyword questions, so memorise register (output capture), ignore_errors (continue after failure), and changed_when (reclassify a change).

Practice All 300-435 Questions

Access 150 questions with complete answers and detailed explanations.

View Full 300-435 Practice Test →

← Back to 300-435 Study Guide