How to use the Ansible apt Module to Manage Linux Packages


VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

On Linux, Ansible is a configuration management tool that works to simplify the provisioning of systems. It uses SSH commands to install software and manage configurations on remote machines. The apt module allows for installation of packages from repositories without explicitly listing them in your inventory file

The “ansible package module” is a command-line tool that allows users to manage Linux packages. The “Ansible apt Module” is the name of the tool.

How to use the Ansible apt Module to Manage Linux Packages

Ansible is a well-known automation software that lets you manage thousands of nodes at once. With the Ansible apt module, you can manage software packages on distant machines, which is one of the most useful capabilities of Ansible.

You may use an apt module to handle packages on Ubuntu or Debian-based computers, such as upgrading them to the most recent version or installing numerous packages on a distant node.

This article will teach you about the Ansible apt module, including what it is, how it works, and how to use it to manage Linux packages on remote systems.


This guide will walk you through the Ansible apt module step by step. If you want to join in, make sure you have the following items:

Related: Ansible Setup Guide (Ubuntu, RHEL, CentOS, macOS)

  • A Linux PC that will be used to test the apt module. An inventory file must be created, and one or more hosts must be configured to perform Ansible commands and playbooks. The example will utilize an inventory group named web and a remote Linux workstation called myserver.

Using the Ansible Apt Module to Install Packages

Let’s start this lesson by installing various packages using ad-hoc commands using the apt module. Ad hoc commands allow you to perform a single command on a remote system quickly and easily.

Run the following command from your Ansible controller. This command connects to the web host using the apt module (-m) and passes an argument (-a) to install the elinks package.

The elinks package is installed by Ansible with the status set to present. The –become option tells the command to execute as a privileged user.

—become ansible web -m apt -a “name=elinks state=present”

You should get a CHANGED message after running the command, indicating that the elinks package has been successfully installed on the remote system.

How-to-use-the-Ansible-apt-Module-to-Manage-LinuxUsing the Ansible apt module, install the elink package.

Then, using an SSH client, check that the elinks package has been installed successfully on the remote node. Run the elinks command to do this. The package installation was successful if the command elinks opens a browser to the website.

1647491840_606_How-to-use-the-Ansible-apt-Module-to-Manage-LinuxChecking for the presence of the elink package.

Within a Playbook, Managing Apt Commands

Installing a single package using an ad-hoc command may be OK, but if you have to install many packages on different systems, it will rapidly become tedious. Instead of using ad-hoc commands, use the ansible-playbook command to combine the Ansible apt module with a playbook.

Creating and Running Ansible Windows Playbooks is related.

Assuming you’ve already signed in to the Ansible controller host, follow these steps:

1. Make an ansible apt module demo directory in your home directory. The playbook you’ll use to launch the apt module will be in this directory.

/ansible apt module demo/mkdir cd /ansible apt module demo/ansible apt module demo

2. In the /ansible apt module demo directory, create a file named my playbook.yml and paste the contents of the following YAML playbook into it.

The playbook below has numerous tasks to install all of the packages necessary to install and maintain the Apache server using the Ansible apt module on the remote system, including:

  • The Apache webserver package itself, apache2.
  • elinks — A web browser for accessing websites hosted by Apache.
  • tree – A program that verifies file structure.
  • zip is a program that zips and unzips files.
  • PHP is a package that allows Apache to execute PHP programs.

—- – example of an Ansible apt module # Specifying the remote server where the package will be installed: myserver remote user: ubuntu # Using Ubuntu as a remote user: genuine tasks: # Setting up the Apache web server on a remote machine ( TASK-1) Install apache httpd (state=present is optional). apache2 is apt’s name. current situation # Installing the zip, tree, and curl packages essential for working with the Apache server ( TASK-2) – name: apt: pkg: install a list of packages – tree – elinks – zip # All packages have been updated to the most recent version ( TASK-3) apt: name: “*” – name: Update all packages to the newest version state: current # Installing php7.2 from an internet.deb ( TASK-4) – name: apt: deb: 7.2.24-0ubuntu0.18.04.8 all.deb state: present # To test the Apache Server, install the curl package ( TASK-5) apt: name: curl update cache: yes apt: name: curl update cache: yes apt: name: curl update cache: yes

3. Now run the playbook to install the packages on the remote computer.

my playbook.yml ansible-playbook

The TASK has a changed status, which means the remote host wasn’t in the right condition and has to be adjusted to perform the command. That TASK has an OK status, indicating that no adjustments are required.

1647491841_415_How-to-use-the-Ansible-apt-Module-to-Manage-LinuxInstalling the elink package using an Ansible playbook.

4. Finally, use the apt list —installed command to see whether all of the packages listed in the my playbook.yml playbook have been installed, as shown below.

grep tree apt list —installed | grep elinks apt list —installed | grep curl apt list —installed

After executing each command, you should see something like this, which verifies that the apt module successfully installed the packages necessary for apache on the remote node.

1647491842_974_How-to-use-the-Ansible-apt-Module-to-Manage-LinuxVerifying the playbook’s execution via log output.

You should also notice the apache2 service active (running) on the remote node if you run the service apache2 status command, as shown below.

1647491843_79_How-to-use-the-Ansible-apt-Module-to-Manage-LinuxChecking to see whether the apache2 service is up and functioning.


Ansible’s apt module is a fantastic method to handle Ubuntu packages on remote machines. It gives you the fastest and most efficient approach to deal with packages remotely.

So, using the Ansible apt module, what packages are you going to install next?

The “ansible apt module with_items” is a command-line tool that allows users to manage packages on Linux systems. The tool can be used to install, remove, upgrade or downgrade packages.

Related Tags

  • ansible apt module example
  • ansible apt install multiple packages
  • ansible yum module
  • ansible apt list
  • ansible apt: upgrade