CompTIA Linux+ (XK0-006)Automation, Orchestration and ScriptingEasy

A system administrator is using Ansible to manage a fleet of Linux servers. They need to ensure a specific service, `httpd`, is running and enabled at boot time on all web servers. Which of the following Ansible playbook snippets correctly achieves this goal?

  1. A```yaml - name: Ensure httpd is running and enabled shell: systemctl start httpd && systemctl enable httpd ```
  2. B```yaml - name: Ensure httpd is running and enabled systemd: name: httpd state: started enabled: yes ```
  3. C```yaml - name: Ensure httpd is running and enabled package: name: httpd state: present ```
  4. D```yaml - name: Ensure httpd is running and enabled service: name: httpd state: started enabled: no ```
Show answer & explanation

Correct answer: B. ```yaml - name: Ensure httpd is running and enabled systemd: name: httpd state: started enabled: yes ```

The `systemd` module (or the more generic `service` module which often defaults to systemd for modern Linux) is the correct and idempotent way to manage services in Ansible. `state: started` ensures it's running, and `enabled: yes` ensures it starts at boot.

Why the other options are wrong

  • A. Using `shell` is generally discouraged for service management in Ansible as it's not idempotent and doesn't provide the same level of error handling or reporting as dedicated modules.
  • C. The `package` module manages software installation, not the running state or boot-time enablement of services.
  • D. `enabled: no` would disable the service at boot, contradicting the requirement.

Ansible Service Management

Ansible's `service` and `systemd` modules are used to manage the state (started, stopped, restarted) and boot-time enablement (enabled, disabled) of system services on target hosts. They ensure idempotent operations.

  • `service` module is generic, `systemd` is specific to systemd-based systems.
  • `state: started` ensures the service is running.
  • `enabled: yes` ensures the service starts automatically at boot.

Memory trick: Service state and enabled, Ansible's command is nailed.

More Automation, Orchestration and Scripting questions