XK0-006 Automation, Orchestration, and Scripting Practice Question
An automation engineer uses Ansible to configure a fleet of Linux servers. A playbook task installs a package and then starts a service, but the service fails to start because the package installs a configuration file only on the first run. The engineer wants the handler to notify only when the package actually changes state. Which Ansible construct ensures the service restart handler fires only when the package task reports a change?
⚠ Common exam trap
The trap here is thinking a handler must be forced to run with `changed_when` rather than simply being notified by a genuinely changed task.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use the `notify` directive on the package task referencing a handler that restarts the service.
Ansible handlers run only when notified by a task that reports `changed`, which is exactly the behavior needed to restart a service only when its package is actually installed or upgraded. The `notify` directive wires the package task to the handler, so idempotent runs that leave the package untouched do not trigger a restart, preventing unnecessary service interruptions across the managed fleet.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Set `run_once: true` on the service handler so it executes a single time per play.
Why it's wrong here
`run_once` limits execution to the first host in the batch, which is unrelated to change detection. The service must restart on each host whose package changed, so restricting it to one host would leave other servers unconfigured. This option confuses execution scope with conditional triggering and does not tie the restart to the package's change state.
- ✓
Use the `notify` directive on the package task referencing a handler that restarts the service.
Why this is correct
Handlers are only invoked when the notifying task reports `changed`, so binding the restart to the package installation via `notify` ensures the service restarts precisely when the package changes. On subsequent idempotent runs where the package is already present, the task reports `ok` and the handler does not fire, matching the requirement to act only on real state changes.
- ✗
Add `changed_when: true` to the package task to guarantee the handler always runs.
Why it's wrong here
`changed_when: true` forces the task to always report changed, which would fire the handler on every run even when nothing changed. That is the opposite of the requirement and breaks idempotence. It also defeats the purpose of handlers, causing unnecessary service restarts across the fleet and potentially causing outages on hosts where no change was needed.
- ✗
Use the `when: ansible_facts.pkg_mgr == 'apt'` conditional on the handler.
Why it's wrong here
Conditioning on the package manager only restricts which hosts run the handler; it does not make the handler fire solely on change. Handlers already trigger only when notified by a changed task. This conditional would skip the restart entirely on non-apt systems, introducing a bug while failing to address the requirement of reacting only to actual package state changes.
Go deeper
Related to this question
Learn chapter
Advanced Shell Scripting and Automation
Key term
Service
A service is a software component or system that performs a specific function and is available to be used by other programs or users over a network.
Key term
Linux
Linux is an open-source operating system that manages computer hardware and software, widely used in servers, desktops, and embedded systems.
About these practice questions
One of 781 original XK0-006 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official CompTIA exam blueprint
This XK0-006 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the XK0-006 exam.