Ansible 101: How to use the Ansible apt Module to Manage Linux Packages


Read more posts by this author.

Ansible is a popular automation platform allowing you to manage thousands of nodes at one time. One of the most valuable features of Ansible is its ability to manage software packages on remote computers with the Ansible apt module.

With an apt module, you can manage Ubuntu or Debian-based machines packages, such as updating the package to the latest version or installing multiple packages on a remote node.

In this tutorial, you’re going to learn what the Ansible apt module is, how it works, and how to use the Ansible apt module to manage Linux packages on remote hosts.


This post will be a step-by-step tutorial on the Ansible apt module. If you’d like to follow along, be sure you have the following in place:

  • A remote Linux computer to test out the apt module on. You’ll need an inventory file set up and one or more hosts already configured to run Ansible command and playbooks on. The remote Linux computer will be called myserver, and the tutorial will use an inventory group called web.

Installing Packages with the Ansible Apt Module

Let’s kick off this tutorial by using the apt module to install some packages using ad-hoc commands. Ad hoc commands are a quick and easy way to run a single command on a remote host.

Log onto your Ansible controller and run the following command. This command uses the apt module (-m) to connect to the host called web and pass an argument (-a) that installs the elinks package.

Ansible installs the elinks package by specifying the state as the present. –become flag allows you to run the command as a privileged user.

ansible web -m apt -a "name=elinks state=present" --become

After you execute the command, you should see a CHANGED message that confirms that the elinks package has been successfully installed on the remote host.

Install the elink package via the Ansible apt module.

Next, log into the remote node using an SSH client and verify the elinks package has been successfully installed. You can do this by running the elinks command. If the command elinks opens a browser on the page, the package installation was successful.

Verifying that the elink package is installed.

Managing apt Commands Within a Playbook

Installing a single package may be OK with an ad-hoc command, but it’s going to get old quickly if you have to install multiple packages on multiple hosts. Instead of using ad-hoc commands, integrate the Ansible apt module with a playbook using the ansible-playbook command.

Assuming you’re already logged into Ansible controller host:

1. Create a directory called ansible_apt_module_demo in your home directory. This directory will contain the playbook you’ll use to invoke the apt module.

mkdir ~/ansible_apt_module_demo
cd ~/ansible_apt_module_demo

2. Open your favorite text editor and create a file called my_playbook.yml in the ~/ansible_apt_module_demo directory and paste in the following YAML playbook contents.

The playbook below contains multiple tasks to install all the packages on the remote machine that are required to install and manage the Apache server using the Ansible apt module, such as:

  • apache2 – The Apache webserver package itself.
  • elinks – A web browser to open websites that Apache hosts.
  • tree – An application to confirm file structure.
  • zip – An application to zip and unzip folders.
  • PHP – A package to run PHP scripts on Apache.
- name: Ansible apt module example
# Defining the remote server where the package will be deployed 
  hosts: myserver
  remote_user: ubuntu   # Using Remote user as ubuntu
  become: true
# Installing the apache web server on remote node ( TASK-1) 
    - name: Install apache httpd  (state=present is optional)
        name: apache2
        state: present

# Installing the zip, tree, and curl packages which are required to work with Apache server ( TASK-2) 
    - name: Install a list of packages
        - zip
        - tree
        - elinks
# Updating all packages to their latest version ( TASK-3) 

    - name: Update all packages to their latest version
        name: "*"
        state: latest
# Installing php7.2 from a .deb on the internet ( TASK-4) 
    - name: install php7.2 from a .deb on the internet
        state: present
# Installing the curl package to test the Apache Server ( TASK-5) 
   - name: Update repositories cache and install "curl" package
       name: curl
       update_cache: yes

3. Now, invoke the playbook and execute the tasks to install the packages on the remote host.

ansible-playbook my_playbook.yml 

Below, you can see that the TASK has a status of changed, meaning the remote host wasn’t in the proper state and was modified to run the command. For that TASK which has ok status, shows they don’t require any changes.

Running an Ansible playbook to install the elink package.

4. Finally, verify if all the packages defined in the my_playbook.yml playbook are installed by running the apt list --installed command separately for each package, as shown below.

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

After running each command, you should see the output something like below, which confirms the successful installation of packages required for apache on the remote node using the apt module.

Verifying the playbook ran via log output.

Also, if you execute the service apache2 status command, you should see the apache2 service active (running) on the remote node, as shown below.

Verifying that the apache2 service is active and running.


The Ansible apt module is a great way to manage your Ubuntu-based packages on remote hosts. It provides you the quick and best way to work with packages remotely.

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

Subscribe to Stay in Touch

Never miss out on your favorite ATA posts and our latest announcements!

Looks like you're offline!