During a configuration-management project you need an Ansible playbook to replace /etc/httpd/conf/httpd.conf on several hosts and ensure that Apache is restarted only when the file is actually updated. Which playbook pattern best satisfies this requirement while preserving idempotency?
Create a task that copies or templates the configuration file with the copy (or template) module and uses notify to call a handler that restarts httpd with the service module.
Use a shell module task to overwrite the file and rely on args: creates=/etc/httpd/conf/httpd.conf, followed by a command task that restarts httpd.
Run the copy module task and then, regardless of change status, execute a command module task that runs "systemctl restart httpd".
Place the restart command inside a raw module task within a block that always runs with when: changed=true.
An idempotent solution relies on a state-aware module to detect whether the destination file changed and, only in that case, trigger a handler. When a copy (or template) task reports a "changed" result it can notify a handler that uses the service module to restart httpd. Handlers run once per play and only if they are notified, preventing unnecessary restarts. By contrast, running shell or command tasks that invoke "systemctl restart httpd" unconditionally forces a change on every run and violates idempotency, even if the file did not change. Using "creates" with shell or raw tasks still misses changes to an existing file, so the restart may be skipped when it is actually required or executed when it is not, making those patterns unsuitable.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What is idempotency in the context of Ansible playbooks?
Open an interactive chat with Bash
How does the 'notify' function work in Ansible?
Open an interactive chat with Bash
Why are shell and command modules less suited for idempotent operations?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access