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

A system administrator is using Ansible to ensure that a specific package, `nginx`, is installed on all web servers. They also need to ensure that the `nginx` service is running and enabled to start automatically on boot. Which Ansible task correctly achieves both of these requirements?

  1. A- name: Ensure Nginx is installed, running, and enabled ansible.builtin.systemd: name=nginx state=started enabled=yes
  2. B- name: Ensure Nginx is installed, running, and enabled ansible.builtin.package: name=nginx state=present ansible.builtin.service: name=nginx state=started enabled=yes
  3. C- name: Ensure Nginx is installed, running, and enabled ansible.builtin.yum: name=nginx state=present ansible.builtin.service: name=nginx state=started enabled=yes
  4. D- name: Install Nginx package: name=nginx state=present - name: Start Nginx service service: name=nginx state=started
Show answer & explanation

Correct answer: B. - name: Ensure Nginx is installed, running, and enabled ansible.builtin.package: name=nginx state=present ansible.builtin.service: name=nginx state=started enabled=yes

To ensure a package is installed and a service is running/enabled, Ansible typically uses two separate modules: `package` (or distributions specific modules like `apt`, `yum`) for package management, and `service` (or `systemd`) for service management. Option C correctly combines the `package` module for installation and the `service` module for starting and enabling `nginx`.

Why the other options are wrong

  • A. The `systemd` module (or `service`) is for managing services, not for installing packages. This task would fail to install `nginx` if it's not already present.
  • C. This option uses `yum` (which is distribution-specific, unlike `package` which is generic) and `service` but implicitly assumes `yum` is available and does not fully leverage the generic `package` module for broader compatibility. However, the core issue is the structure: two distinct tasks are needed, not two modules within one task. The example shows two modules in one task, which is incorrect YAML syntax for Ansible.
  • D. This option uses two separate tasks but does not include `enabled=yes` for the service, so it won't ensure automatic startup on boot. Also, it uses the shorthand module name `package` and `service`, which is fine but less explicit than `ansible.builtin.*`.

Ansible Package & Service Management

Ansible manages packages using the `package` module (or specific ones like `apt`, `yum`) and services using the `service` (or `systemd`) module. These are often combined in playbooks to ensure applications are both installed and running correctly.

  • `package: name=pkg_name state=present` installs a package.
  • `service: name=svc_name state=started` starts a service.
  • `service: name=svc_name enabled=yes` ensures service starts on boot.
  • Often require `become: yes` for root privileges.

Memory trick: Packages install, services then enthrall.

More Automation, Orchestration and Scripting questions