Skip to main content
Ansible Configuration
CHAPTER 08

Managing Packages and Services

Updated: May 15, 2026
25 min read

# CHAPTER 8

Managing Packages and Services

1. Introduction

The fundamental requirement of any server configuration is software installation and lifecycle management. Whether you are provisioning a simple Nginx proxy or deploying a complex, multi-tier application stack, you must interact with the operating system's package managers (APT, YUM, DNF) and service managers (systemd). In this chapter, we will master the core Ansible modules responsible for software orchestration, learning how to ensure packages are installed, updated, or removed, and guaranteeing that critical background services are running and enabled on boot.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Utilize the apt and yum modules for OS-specific package management.
  • Utilize the generic package module for OS-agnostic automation.
  • Manage system daemon lifecycles using the service module.
  • Understand the difference between state: started and enabled: yes.
  • Architect a multi-step task sequence to deploy a web application stack.

3. Beginner-Friendly Explanation

Imagine setting up a new smart TV.
  • Package Management (apt / yum): The App Store. You navigate to the store and click "Download Netflix." In Ansible, you write a task telling the server to download the Nginx software from the internet.
  • Service Management (service): The Power Button and Settings. Just because you downloaded Netflix doesn't mean it's playing a movie. You have to open the app (state: started). Furthermore, you go into the TV settings and say, "Every time I turn on the TV, automatically open Netflix" (enabled: yes).

Ansible handles both downloading the software and controlling its behavior.

4. Package Management Modules

When managing Linux, you interact with repositories. For Debian/Ubuntu, use apt. For RedHat/CentOS, use yum (or dnf).

Installing a specific package:

yaml
12345
- name: Ensure Apache is installed on Ubuntu
  apt:
    name: apache2
    state: present # Ensures it is installed.
    update_cache: yes # Runs 'apt-get update' before installing

State Options:

  • present: Install it (if it isn't already).
  • latest: Install it, and if it is installed, upgrade it to the newest version.
  • absent: Delete it from the server.

The Generic package Module: If you manage a mixed fleet of Ubuntu and CentOS servers, writing conditional tasks for apt and yum is tedious. Ansible provides a smart module called package. It detects the OS automatically!

yaml
1234
- name: Install Git on ANY Linux OS
  package:
    name: git
    state: present

5. Service Management Module

Once the software is installed, you must manage its daemon (the background process). We use the service (or systemd) module.
yaml
12345
- name: Ensure the Apache service is running and enabled
  service:
    name: apache2
    state: started # Start it right now
    enabled: yes   # Start it automatically if the server reboots

State Options:

  • started: Turn it on.
  • stopped: Turn it off.
  • restarted: Turn it off and immediately back on (useful when configuration files change).

6. Mini Project: Deploy LAMP Stack Automatically

Let's combine these modules to deploy a full Linux, Apache, MySQL, and PHP (LAMP) stack in a single playbook execution.

Step-by-Step Architecture Concept:

yaml
12345678910111213141516171819202122232425262728293031
---
- name: Deploy LAMP Stack on Ubuntu
  hosts: webservers
  become: yes

  tasks:
    # 1. We can pass a LIST of packages to install them all at once!
    - name: Install Apache, MySQL, and PHP
      apt:
        name: 
          - apache2
          - mysql-server
          - php
          - libapache2-mod-php
          - php-mysql
        state: present
        update_cache: yes

    # 2. Ensure Apache is running
    - name: Start and Enable Apache Service
      service:
        name: apache2
        state: started
        enabled: yes

    # 3. Ensure MySQL is running
    - name: Start and Enable MySQL Service
      service:
        name: mysql
        state: started
        enabled: yes

*Run this playbook. In 60 seconds, a blank Ubuntu server will be transformed into a fully operational web hosting platform.*

7. Real-World Scenarios

A company experienced a massive power outage in their data center. When the power was restored, the 20 Linux servers booted up, but the website remained offline. The manual administrators panicked. They realized that while the Nginx software was installed, nobody had ever run systemctl enable nginx when they built the servers two years ago. Therefore, when the servers rebooted, Nginx stayed off. The DevOps engineer rewrote the provisioning scripts using Ansible, strictly mandating the enabled: yes argument on every service module task. During the next reboot test, all servers booted, and all web services launched automatically without human intervention.

8. Best Practices

  • Use Lists for Packages: In the mini-project, notice how we passed a YAML list (- apache2, - php) to the name: argument. This is vastly superior to writing 5 separate apt tasks. Ansible will compile the list and run a single apt-get install apache2 php mysql-server command, which executes 5x faster than installing them individually.

9. Security Recommendations

  • Uninstalling Default Software: Package management isn't just for installing; it is critical for security hardening. Many cloud OS images come with default software (like telnet or ftp) that are notoriously insecure. A mature DevOps pipeline includes a playbook that runs immediately on all new servers, using state: absent to aggressively delete these vulnerable packages.

10. Troubleshooting Tips

  • Package Name Discrepancies: The generic package module is smart, but it cannot fix naming differences between OS families. For example, the Apache web server is named apache2 on Ubuntu, but httpd on CentOS. If you use the generic module and ask it to install apache2 on CentOS, it will crash with a "Package not found" error. You must still use variables or Facts to handle differing package names.

11. Exercises

  1. 1. What is the operational difference between the present and latest states in the apt module?
  1. 2. Why is the enabled: yes argument critical for infrastructure resilience?

12. FAQs

Q: Does Ansible use systemctl or service commands on the backend? A: Ansible is smart enough to detect the init system of the target Linux machine. If the machine is modern (Ubuntu 16.04+), the service module automatically translates the request into a systemd / systemctl API call.

13. Interview Questions

  • Q: Explain how Ansible ensures Idempotency when executing a package installation task using the apt module with state: present.
  • Q: An engineer provides you with an Ansible playbook containing 10 consecutive tasks utilizing the yum module to install 10 distinct packages. How would you optimize this playbook for execution speed?

14. Summary

In Chapter 8, we established the core competencies of configuration management. We utilized the apt and yum modules to orchestrate software installations, learning how to leverage YAML lists to execute bulk installations efficiently. We moved beyond simple installation by mastering the service module, ensuring that our deployed applications are actively running and resilient to unexpected server reboots. By combining these concepts, we successfully architected an automated workflow capable of deploying a complex, multi-tiered LAMP stack from scratch.

15. Next Chapter Recommendation

We installed the web server, but it's serving the default "Welcome to Nginx" page. How do we push our custom HTML and configuration files to the server? Proceed to Chapter 9: File Management and Templates.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·