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.

Watch Video

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

What You'll Learn

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

Read the full written article: Ansible apt Module: Install Packages on Ubuntu/Debian (Examples)

Topics Covered

Related Video Tutorials