AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible apt Module: Install Packages on Ubuntu/Debian (Examples)

By Luca Berton · Published 2024-01-01 · Category: installation

How to install, remove, and update packages on Ubuntu, Debian, and Linux Mint using Ansible's apt module. Includes update_cache, state, and version pinning examples.

How to Install a package with Ansible in Debian-like systems? I’m going to show you a live Playbook with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot

Ansible Install a package in Debian-like systems Today we’re talking about the Ansible module APT. The full name is “ansible.builtin.apt” which means is part of the collection of modules “builtin” with ansible and shipped with it. This module is pretty stable and out for years. It works on Debian-like operating systems and Manages packages with the apt package manager. It’s similar to the yum or DNF module for RedHat-like operating systems.

Main Parameters • name _string_ • state _string_ • update_cache _boolean_

The parameter list is pretty wide but this three are the most important options. In the “name” parameter you are going to specify the name of the package or the specific version you would like to install. The state specifies the action that we would like to perform. In our case for install is “present”. “update_cache” forces to update the repository metadata before the installation. It could be useful to make sure that the repository is up-to-date.

Demo

Let’s jump in a real-life playbook to install a package in Debian-like systems with Ansible

code with ❤️ in GitHub

Conclusion Now you know how to install a package and a specific version of a package in Debian-like systems.

Advanced apt Module Usage

Install multiple packages

Install a specific version

Update all packages (equivalent to apt upgrade)

Remove a package

Install from a .deb file

Add an APT repository and install

Key Parameters Reference

| Parameter | Type | Description | |-----------|------|-------------| | name | string/list | Package name(s) | | state | string | present, absent, latest, fixed | | update_cache | bool | Run apt update before install | | cache_valid_time | int | Seconds before cache is considered stale | | upgrade | string | dist, full, safe, yes | | purge | bool | Remove config files with absent | | deb | string | Path or URL to .deb file | | force_apt_get | bool | Use apt-get instead of aptitude | | autoremove | bool | Remove unused dependencies |

FAQ

What's the difference between apt and package module?

ansible.builtin.apt is Debian/Ubuntu-specific with full apt features. ansible.builtin.package is generic and works across distros but has fewer options. Use apt when you know the target is Debian-based.

How do I handle "dpkg lock" errors?

How do I pin a package version to prevent upgrades?

Related ArticlesAnsible Become GuideAnsible Roles Guide

Category: installation

Watch the video: Ansible apt Module: Install Packages on Ubuntu/Debian (Examples) — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home