You can use the Ansible apt module to manage packages that use the apt package manager on Debian-based Linux distributions, such as Ubuntu. The module allows you to perform all basic package management operations, such as installing, removing, and updating the packages.

Prerequisites

Two prerequisites must be met to follow this guide:

  • An Ansible control node: A server running Ubuntu with Ansible installed and configured
  • An Ansible target node: A server running Ubuntu 22.04

Create an inventory file

The Ansible inventory file is used to define a single remote host or a group of them that you want to manage from the Ansible control node. For the purpose of this tutorial, we will create an inventory file on the Ansible control node.

First, create a new project directory and create an inventory file inside the ~/project directory:

mkdir ~/project
nano ~/project/inventory.txt

Add the following lines to define the target node's IP address, SSH username, and password.

target1 ansible_host=192.168.0.11 ansible_user=root ansible_ssh_pass=secure_root_password

Install a package using the apt module

In this section, I show how to install the unzip package using the apt module. To install a new package, you will need to specify the package name and the state of the package.

The following example will update the package cache and check whether the unzip package is installed on the target server. If it is not installed, it will be installed. If it is already installed, Ansible will do nothing.

First, create a playbook.yml file inside the ~/project directory:

nano ~/project/playbook.yml

Add the following lines to define the apt module and package name that you want to install on the target server.

- hosts: all
  tasks:
  - name : Install unzip package
    apt: name=unzip state=present update_cache=true

A brief explanation of the playbook.yml file is shown below:

  • apt specifies the module to be installed
  • name=unzip is the name of the package you want to install
  • state=present will install the package if it is not installed
  • update_cache=true will update the package cache if it is out of date

Next, change the directory to the ~/project and run the Ansible playbook by specifying an inventory file.

cd ~/project
ansible-playbook playbook.yml -i inventory.txt

This command will install the unzip package on the target server, as shown below.

Installing the unzip package on the target server

Installing the unzip package on the target server

Install the latest package version

In this section, we will install the latest Nginx package using the apt module.

In the previous section, we used state=present to check whether the package was installed. Now, we will use state=latest to install the latest version of the package.

Let's create a new playbook to install the latest version of Nginx on the target server.

nano ~/project/playbook.yml

Add the following lines to define the apt module and package name:

- hosts: all
  tasks:
  - name : Install the latest Nginx package
    apt: name=nginx state=latest update_cache=true

Save and close the file, and then run the Ansible playbook using the following command:

ansible-playbook playbook.yml -i inventory.txt

This command will install the latest Nginx package on the target server, as shown below:

Installing the latest Nginx package on the target server

Installing the latest Nginx package on the target server

Install multiple packages

In some cases, you will need to install multiple packages on the target server. In this case, you can use the apt module with a loop and combine all packages in a single task.

Let's create a new playbook to install the git, wget, unzip, and curl packages on the target server.

nano ~/project/playbook.yml

Add the following lines to define the apt with a loop module to install multiple packages:

- hosts: all 
  tasks:
  - name : Install multiple packages
    apt: name={{ item }} state=latest update_cache=true
    loop: [git, wget, unzip, curl]

Save and close the file, and then execute the Ansible playbook using the following command:

ansible-playbook playbook.yml -i inventory.txt

This command will install all the packages specified in a playbook on the target server, as shown below:

Installing multiple packages on the target server

Installing multiple packages on the target server

Update all packages

You can update all packages on the target server using the state=latest and force_apt_get=true parameters.

Let's create a playbook to update all packages on the target server.

nano ~/project/playbook.yml

Add the following lines to a file:

- hosts: all
  tasks:
  - name : Update all packages
    apt: name="*" state=latest update_cache=true force_apt_get=true

Save a file, and then run the Ansible playbook with the following command:

ansible-playbook playbook.yml -i inventory.txt

This command will update the packages on the target server, as shown below:

Updating all packages on the target server

Updating all packages on the target server

Install a .deb package

Sometimes, you will need to download the .deb package file and install it on the target server. In this case, the Ansible apt module will help you download the .deb package from the web and install it on the target server.

In this example, we will create a playbook to download the Puppet deb package file and install it on the target server.

nano ~/project/playbook.yml

Add the following lines to define the apt module with the Puppet package URL:

- hosts: all
  tasks:
  - name : Install package from .deb file
    apt: deb=https://apt.puppetlabs.com/puppet6-release-jammy.deb state=present update_cache=true

Save and close the file, and execute the Ansible playbook with the following command:

ansible-playbook playbook.yml -i inventory.txt

This command will download the Puppet package and install it on the target server.

Installing the .deb package on the target server

Installing the .deb package on the target server

Remove a package

You can use the state=absent parameter with the Ansible apt module to remove the specified package from the remote server.

Let's create a playbook to uninstall the Nginx package from the target server.

nano ~/project/playbook.yml

Add the following lines to define the apt module with the absent parameter:

- hosts: all
  tasks:
  - name : Uninstall nginx package
    apt: name=nginx state=absent

Save and close the file, and then run the Ansible playbook with the following command:

ansible-playbook playbook.yml -i inventory.txt

This command will remove the Nginx package from the target server:

Removing the Nginx package from the target server

Removing the Nginx package from the target server

Remove unused dependencies

You can use the autoremove=true parameter with the apt module to remove any dependencies that were installed along with other packages and are no longer needed.

Let's create a playbook to remove unused dependencies on the target server.

nano ~/project/playbook.yml

Add the following lines to define the apt module with the autoremove=true parameter.

- hosts: all
  tasks:
  - name : Remove unused dependencies
    apt: autoremove=true

Save the file, and then run the Ansible playbook to execute the task:

ansible-playbook playbook.yml -i inventory.txt

This command will remove all unused dependencies from the target server.

Removing unused dependencies from the target server

Removing unused dependencies from the target server

Clean up the package cache

When you install any packages on a Debian-based system, it will download all packages and store them inside the /var/cache/apt/archives/ directory. It is a good idea to remove the entire package cache after package installation to save disk space.

You can use the autoclean=true parameter with an apt module to clean up the cache from the apt.

Let's create a playbook to remove and clean up the package cache on the target server.

nano ~/project/playbook.yml

Add the following lines to define the apt module with the autoclean=true parameter.

- hosts: all
  tasks:
  - name : Clean Up the Cache
    apt: autoclean=true

Save and close the file, and then run the Ansible playbook command to clean up the cache.

ansible-playbook playbook.yml -i inventory.txt

This command will clean up the package cache on the target server.

Cleaning up the package cache on the target server

Cleaning up the package cache on the target server

Capture the output of the apt module

When you run any command using the Ansible playbook, it cannot show you the output of the executed command. You can use the register=apt_output parameter with the apt module to print the output of any command executed in the playbook. It will help you to diagnose the problem in the event of playbook task failure.

In this example, we will create a playbook to install the tree package and capture the output:

nano ~/project/playbook.yml

Add the following lines to define the apt module with the register=apt_output parameter.

- hosts: all
  tasks:
  - name: Capture the Output
    apt: name=tree state=present update_cache=true
    register: apt_output
  - debug: var=apt_output

Save and close the file, and then run the Ansible playbook using the following command:

ansible-playbook playbook.yml -i inventory.txt

This command will install the tree package on the target server and print the execution output, as shown below:

Showing the output of the apt module

Showing the output of the apt module

Validate whether a package is installed

In some cases, you may need to check whether the required package is installed without making any modifications to the system. In that case, you can use package_facts with an apt module to validate the package information.

Let's create a playbook to check whether the Nginx package is installed on the target server:

nano ~/project/playbook.yml

Add the following lines:

- hosts: all
  tasks:
  - name: Gather Package facts
    package_facts: manager=auto
  - name : Validate whether Nginx is installed or not.
    debug: msg="Nginx is installed"
    when: "'nginx' in ansible_facts.packages"
  - name : Validate whether Nginx is installed or not.
    debug: msg="Nginx is not installed"
    when: "'nginx' not in ansible_facts.packages"

Save and close the file, and then run the Ansible playbook with the following command:

You should get the following output if Nginx is not installed on the target server:

Validating Nginx package installation

Validating Nginx package installation

Add a repository and a GPG key

In this section, we will show you how to use the apt_repository module to add the repository and the apt_key module to add the GPG key to the target system.

Let's create a playbook to add the Docker GPG key and a Docker repository and then install the Docker package on the target server.

nano ~/project/playbook.yml

Add the following lines to define the package dependencies, Docker GPG key URL, and Docker repository path.

- hosts: all
  tasks:
  - name : Install GPG Dependency
    apt: name=gnupg2 state=latest
  - name : Install Python Dependency
    apt: name=python3-apt state=latest
  - name : Add the Docker GPG apt Key
    apt_key: url=https://download.docker.com/linux/ubuntu/gpg state=present
  - name : Add the Docker Repository
    apt_repository: repo="deb https://download.docker.com/linux/ubuntu jammy stable" state=present
    register: apt_repository_output
  - name : Update apt cache and install docker-ce
    apt: update_cache=yes name=docker-ce state=latest

Save and close the file, and then run the Ansible playbook using the following command:

ansible-playbook playbook.yml -i inventory.txt

This command will install the required dependencies, add the Docker GPG key and repository, and then install the Docker package on the target server.

Adding Docker GPG key and repository and installing the Docker packages on the target server

Adding Docker GPG key and repository and installing the Docker packages on the target server

Remove a repository

If you want to remove the repository from apt, you can use the state=absent parameter with the apt_repository module.

Let's create a playbook to remove the Docker repository from the target server.

nano ~/project/playbook.yml

Add the following lines to define the apt_repository module with the Docker repository path:

- hosts: all
  tasks:
  - name: Remove the Docker Repository
    apt_repository: repo="deb https://download.docker.com/linux/ubuntu jammy stable" state=absent

Save and close the file, and then run the Ansible playbook with the following command:

ansible-playbook playbook.yml -i inventory.txt

This command will remove the Docker repository from the target server.

Subscribe to 4sysops newsletter!

Removing the Docker repository from the target server

Removing the Docker repository from the target server

Conclusion

In this guide, I showed you how to use the Ansible apt module to manage packages on the Ubuntu and Debian operating systems. We also explained the use of the Ansible apt_repository module to manage the repository on Debian-based operating systems and how to use different Git Bash commands to manage and track your repositories. I hope the Ansible apt module provides the easiest way to manage packages on the remote system. For more information, visit the Ansible apt module documentation page.

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account