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) — Video Tutorial
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.
What You'll Learn
- How to Install a package with Ansible in Debian-like systems?
- Ansible Install a package in Debian-like systems
- Main Parameters
- Demo
- Conclusion
- Advanced apt Module Usage
- Install multiple packages
- Install a specific version
- Update all packages (equivalent to apt upgrade)
- Remove a package
Full Tutorial Content
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
```yaml
---
- name: module apt Playbook
hosts: all
become: true
tasks:
- name: install package
apt:
name: curl
state: present
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/install%20a%20package%20in%20Debian-like%20systems)
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
```yaml
- name: Install multiple packages
ansible.builtin.apt:
name:
- curl
- wget
- git
- htop
- vim
state: present
update_cache: true
become: true
```
Install a specific version
```yaml
- name: Install specific nginx version
ansible.builtin.apt:
name: nginx=1.24.0-1~jammy
state: present
become: true
```
Update all packages (equivalent to apt upgrade)
```yaml
- name: Update all packages
ansible.builtin.apt:
upgrade: dist
update_cache: true
cache_valid_time: 3600 # Only update cache if older than 1 hour
become: true
```
Remove a package
```yaml
- name: Remove package
ansible.builtin.apt:
name: apache2
state: absent
become: true
- name: Remove package and its config files (purge)
ansible.builtin.apt:
name: apache2
state: absent
purge: true
become: true
```
Install from a .deb file
```yaml
- name: Download and install .deb package
ansible.builtin.apt:
deb: https://example.com/package.deb
become: true
```
Add an APT repository and install
```yaml
- name: Add Docker GPG key
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
become: true
- name: Add Docker repository
ansible.built
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 4 min
- Category: installation
Read the full written article: Ansible apt Module: Install Packages on Ubuntu/Debian (Examples)
Related Video Tutorials
- Install Docker in Debian-like systems - Ansible module apt_key, apt_repository and apt — How to automate the installation of the docker-ce engine in Ubuntu 20.04 LTS x86_64 (or amd64) using Ansible Playbook. The procedure is going to take care of the GPG signing key, add a repository into the sources list, and install the latest docker-ce package. Included Demo for Debian-like workstation (Debian and Ubuntu).
- Install Google Chrome in Debian like systems - Ansible module apt_key, apt_repository and apt — How to install the latest Google Chrome Stable on a Debian-like workstation (Debian, Ubuntu, Linux Mint, MX Linux, Deepin, AntiX, PureOS, Kali Linux, Parrot OS, Devuan, Knoppix, AV Linux Linux) verify software using the public GPG key and set up the Google repository. Included Playbook in Ubuntu 20.04 LTS.
- Install Microsoft Edge on Debian with Ansible — Learn how to install Microsoft Edge on Debian using Ansible. Follow our guide for a smooth setup process, including repository and key additions.
- Install PostgreSQL in Debian-like systems - Ansible modules apt, stat, shell, service — How to automate the installation of PostgreSQL on Debian-like systems: installing the necessary packages and dependency, initializing the configuration, starting and enabling service on boot using Ansible Playbook and apt, stat, shell, and service modules.
- Install Spotify snap in Debian-like systems - Ansible module snap — How to automate the installation of Spotify snap system-wide in Debian-like systems using Ansible module snap.
- Install Zoom flatpak in Debian-like systems - Ansible module flatpak — How to automate the installation of Zoom flatpak system-wide in Debian-like systems using Ansible module flatpak.