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.
Table of Contents
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:
- An Ansible controller host – This tutorial will be using Ansible v2.9.24 on an Ubuntu 18.04.5 LTS machine.
- A remote Linux computer to test out the
aptmodule 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.
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 google.com opens a browser on the google.com page, the package installation was successful.
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 tasks: # Installing the apache web server on remote node ( TASK-1) - name: Install apache httpd (state=present is optional) apt: 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 apt: pkg: - zip - tree - elinks # Updating all packages to their latest version ( TASK-3) - name: Update all packages to their latest version apt: name: "*" state: latest # Installing php7.2 from a .deb on the internet ( TASK-4) - name: install php7.2 from a .deb on the internet apt: deb: http://security.ubuntu.com/ubuntu/pool/main/p/php7.2/php7.2_7.2.24-0ubuntu0.18.04.8_all.deb state: present # Installing the curl package to test the Apache Server ( TASK-5) - name: Update repositories cache and install "curl" package apt: name: curl update_cache: yes
3. Now, invoke the playbook and execute the tasks to install the packages on the remote host.
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.
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.
Also, if you execute the
service apache2 status command, you should see the apache2 service active (running) on the remote node, as shown below.
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?
More from Adam The Automator & Friends
Expose REST APIs, schedule jobs, and provide custom user interfaces with the platform built for PowerShell to integrate with anything.
We’ve put together a list of the resources we, at ATA, can wholeheartedly recommend!
Why not write on a platform with an existing audience and share your knowledge with the world?