Ansible apt Module: Install Packages on Ubuntu/Debian

Ansible apt Module

The Ansible apt module helps manage packages on Debian-based systems like Ubuntu, Debian, and Linux Mint. This module is excellent for installing, updating, removing, or upgrading software packages. It also manages package repositories and removes a package or installs a specific version. The apt module ensures all necessary packages are installed and up-to-date and removes unwanted software.

In this guide, we’ll explore how to install Debian/Ubuntu packages using the Ansible apt module.

Understanding the Ansible Apt Module

The Ansible apt module manages packages on Debian-based systems using the Advanced Package Tool (APT). It performs various package management tasks such as installing, upgrading, and removing packages.

Here are some key options for the apt module:

  • name: The name of the package to manage.
  • state: The desired state of the package. Common values include present, absent, latest, hold, and unhold.
  • update_cache: If set to yes, a boolean value will update the APT package cache before performing any package operations.
  • upgrade: Specifies the type of upgrade to perform. Valid values are yes, safe, full, dist, and none.

Using apt Module in Ad-Hoc Commands

Ansible ad-hoc commands are a quick way to run simple tasks without writing a playbook. This is useful for testing or performing single operations. To install packages using ad-hoc commands, you can use the ansible command directly from the command line.

1. Installing a Single Package

Use the below command to install the nginx package on all hosts defined in the inventory file.

 # ansible all -m apt -a "name=nginx state=present" --become

2. Updating the Package Cache

Use this command to update the APT package cache on all remote servers.

 # ansible all -m apt -a "update_cache=yes" --become

3. Installing Multiple Packages

To install multiple packages (e.g., Nginx, git, and curl), use the following command.

 # ansible all -m apt -a "name=nginx,git,curl state=present" --become

4. Removing a Single Package

To remove a package, use the state: absent option.

 # ansible all -m apt -a "name=nginx state=absent" -b

Using apt Module in Playbooks

While ad-hoc commands are helpful for quick tasks, playbooks provide a more structured and repeatable way to manage configurations. Here, we will write playbooks to perform various package management tasks.

1. Installing a Single Package

To install a single package, such as nginx, create a playbook named install_nginx.yml.

---
- name: Install Nginx on Debian/Ubuntu Servers
  hosts: all
  become: yes
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

This playbook installs the Nginx package on all remote servers specified in your inventory file.

2. Installing Multiple Packages

Modify the playbook to include a list of packages to install multiple packages.

---
- name: Install Multiple Packages on Debian/Ubuntu Servers
  hosts: all
  become: yes
  tasks:
    - name: Install Multiple Packages
      apt:
        name:
          - nginx
          - git
          - curl
        state: present

This playbook installs the nginx, git, and curl packages on all remote hosts specified in the inventory file.

3. Updating Packages

To update all packages on the system, you can use the update_cache and upgrade options.

---
- name: Update Packages on Debian/Ubuntu Servers
  hosts: all
  become: yes
  tasks:
    - name: Update APT Package Cache
      apt:
        update_cache: yes

    - name: Upgrade All Packages
      apt:
        upgrade: dist

This playbook performs a distribution upgrade, updating all packages to their latest versions.

4. Removing Packages

To remove a package, set the state to absent.

---
- name: Remove Nginx from Debian/Ubuntu Servers
  hosts: all
  become: yes
  tasks:
    - name: Remove Nginx
      apt:
        name: nginx
        state: absent

This playbook uses the state: absent option to ensure that the specified package (nginx) is removed from the system.

5. Holding a Package

To prevent a package from being upgraded, you can hold it using the following playbook.

---
- name: Hold Nginx Package on Debian/Ubuntu Servers
  hosts: all
  become: yes
  tasks:
    - name: Hold Nginx Package
      apt:
        name: nginx
        state: hold

This playbook uses the state: hold option to ensure that the specified package (nginx) is held and not upgraded during system updates.

6. Install a .deb Package

Sometimes, you need to install a specific .deb package that is not available in the repositories. This playbook installs the google-chrome-stable_current_amd64.deb package.

---
- name: Install a Chrome .deb Package
  hosts: all
  become: yes
  tasks:
    - name: Download Chrome .deb package
      get_url:
        url: https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
        dest: /tmp/google-chrome-stable_current_amd64.deb

    - name: Install Chrome .deb package
      apt:
        deb: /tmp/google-chrome-stable_current_amd64.deb

The above playbook:

  • Uses the get_url module to download the google-chrome-stable_current_amd64.deb package to the /tmp directory.
  • Then, use the apt module to install the downloaded .deb package.

7. Remove Unused Dependencies

Unused dependencies can accumulate on a system over time. This example shows how to remove these unnecessary packages.

---
- name: Remove Unused Dependencies
  hosts: all
  become: yes
  tasks:
    - name: Remove unused dependencies
      apt:
        autoremove: yes

This playbook uses the autoremove: yes option to remove packages that were automatically installed to satisfy dependencies for other packages and are no longer needed.

8. Clean Up the Package Cache

Cleaning up the package cache helps free up disk space and remove outdated packages. This example demonstrates how to clean the APT cache.

---
- name: Clean Up Package Cache
  hosts: all
  become: yes
  tasks:
    - name: Clean APT cache
      apt:
        autoclean: yes

This playbook uses the autoclean: yes option to remove package files from the cache that can no longer be downloaded (obsolete).

Conclusion

In this guide, we explored how to use Ansible to manage Debian/Ubuntu packages using the apt module. By leveraging Ansible’s automation capabilities, you can efficiently manage packages on your Debian/Ubuntu systems, ensuring consistency and reducing manual intervention.

FAQs

1. How do I install a package using the APT module in Ansible?

To install a package, use: ansible all -m apt -a 'name=package_name state=present' to install the desired package on all targeted hosts.

2. Is it possible to install multiple packages at once with the APT module?

Yes, you can install multiple packages by specifying them in a list: ansible all -m apt -a 'name=package1,package2 state=present'.

3. Can I specify a particular version of a package using the APT module?

Yes, you can specify a version by adding it to the package name: ansible all -m apt -a 'name=package_name=version state=present'.

4. How can I install a package and ignore missing dependencies using the APT module?

You can add the force=yes parameter to ignore missing dependencies: ansible all -m apt -a 'name=package_name state=present force=yes'.

About Hitesh Jethva

I am Hitesh Jethva, Founder and Author at Code2DevOps.com. With over 15 years of experience in DevOps and open source technologies, I am passionate about empowering teams through automation, continuous integration, and scalable solutions.

View all posts by Hitesh Jethva