XK0-006 Automation, Orchestration, and Scripting Practice Question
A configuration-management playbook run by an administrator must install the nginx package only on hosts whose inventory group is webservers, and must restart the nginx service whenever the package installation changes the system. The administrator is using Ansible. Which pair of task attributes achieves both the conditional execution and the conditional restart?
⚠ Common exam trap
Many exam-takers confuse tags with conditionals; tags gate tasks by command-line selection, while when: evaluates host-specific data during the run.
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 when: "'webservers' in group_names" on the install task and a notify: directive referencing a handler that restarts nginx.
Ansible conditionals evaluate Jinja2 expressions against host facts and magic variables. group_names is a list of the inventory groups containing the current host, so testing membership restricts the install to webservers hosts. The notify: attribute links a task to a handler; handlers are deferred and fire only when the notifying task returns changed, which is exactly the behavior needed to restart nginx solely after a real package change.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use when: "inventory_hostname == 'webservers'" on the install task and a notify: directive referencing a handler that restarts nginx.
Why it's wrong here
inventory_hostname holds the host's own name, not its group membership, so comparing it to the literal string 'webservers' matches only a host actually named webservers. All other webservers-group members would be skipped. The correct variable for group membership is group_names, making this conditional logically wrong despite the valid handler pattern.
- ✓
Use when: "'webservers' in group_names" on the install task and a notify: directive referencing a handler that restarts nginx.
Why this is correct
The when: conditional evaluates the group_names variable, which contains the inventory groups the host belongs to, so the install task runs only on webservers hosts. The notify: directive queues a handler named in the handlers section, and handlers run only when the notifying task reports a changed state. Together they deliver conditional installation plus restart-on-change.
- ✗
Use tags: webservers on the install task and a notify: directive referencing a handler that restarts nginx.
Why it's wrong here
Tags select which tasks run based on --tags or --skip-tags at invocation time; they do not inspect host membership. A host in a different group would still run the tagged task if the play targets it. Tags therefore do not implement the group-based conditional the administrator needs, even though the notify and handler portion is correct.
- ✗
Use when: "'webservers' in group_names" on the install task and a changed_when: true attribute that restarts nginx.
Why it's wrong here
changed_when: controls how Ansible interprets a task's changed status; it does not perform actions. Setting it to true merely forces the task to report changed even when nothing happened, which can cause false handler triggers. It cannot restart a service, so the restart requirement is unmet and the reporting becomes misleading.
Go deeper
Related to this question
About these practice questions
Courseiva writes every XK0-006 question from scratch — 781 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.