Introduction to Ansible APT Package and Repository

APT or Advanced Packaging Tool is the preferred package management toolset in Ubuntu. It allows you to install new packages, update them and remove the packages. In this session, we will go through 3 APT related command line tools which are helpful and their Ansible counterparts.

Apt-get – All the basic package management operations can be done using this tool. Ansible apt-get module provides this functionality. For more details refer the Ansible docs.

Apt-add-repository – This tool is used for adding a new repository to the repository list. The default repository may not have the latest version of all the packages. So you may need to add additional repositories for some software maintainers. Ansible apt_repository module provides the functionality for adding a new repository.

Apt-key – This tool is used to manage the list of keys for authenticating apt packages. Ansible Apt_key module is used to manage the keys.

Prerequisite on the target servers:

  • python-apt (Python 2)
  • python3-apt (Python 3)
  • aptitude

1. Installing new apt packages

For installing new packages you have to give the name of the package in name parameter and the desired state of the package, in this case, present, in the state parameter. The default state is present. But I still include it for clarity. Also, it is better to set the update_cache to true. Thus you can ensure the indexes are synchronized with the sources list. It is the same like running the apt-get update command before installing a package.

The following example will do a cache update to synchronize the index, check if the ‘zip’ package is installed on the target server. And if it is not installed the package will be installed. If the package is already installed, then it won’t be upgraded.

- hosts: loc
  tasks:
    - name: Ansible apt install packages example
      apt:
        name: zip
        state: present 
        update_cache: true

1.1 Installing the latest version of package

As mentioned in the previous example, if you set the state to ‘present’, then Ansible will only check if the package is present. So if a newer package is available it won’t be installed. If you want the latest apt package to be installed, then you can set state parameter to latest. This will ensure the package with the latest version is installed. The following example will update the cache first, then install the latest package of zip.

- hosts: loc
  tasks:
    - name: Ansible apt install latest version example
      apt:
        name: zip
        state: latest
        update_cache: true

1.2 Ansible install multiple packages using with_items

Instead of writing multiple tasks to install packages, you can use with_items and combine those. In the following example, I am installing 3 packages: git, nginx, and docker-ce.

Note 1: You should write the with_items block on the same level as ‘apt’ module and not its arguments. Else Ansible will throw an error. For more details on with_items refer Ansible docs.

- hosts: loc
  tasks:
  - name: Ansible apt with_items example
    apt:
      name: "{{ item }}"
      update_cache: true
      state: present
    with_items:
      - 'git'
      - 'nginx'
      - 'docker-ce'

1.3 Ansible apt ad-hoc

You can also use the ad-hoc method to install new packages using the apt module.

ansible all -m apt -a "name=nginx state=absent" -i inventory.ini

2. Removing apt packages

You can remove packages using APT module by setting the state parameter to absent. The following example will remove the zip package. Since the module is idempotent, it wouldnt throw an erro if the apcakge is not present.

- hosts: loc
  tasks:
  - name: Ansible apt remove package example
    apt:
      name: zip
      state: absent