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
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 Articles • Ansible Become Guide • Ansible Roles Guide
Category: installation
Watch the video: Ansible apt Module: Install Packages on Ubuntu/Debian (Examples) — Video Tutorial