Ansible apt_repository Module Tutorial + Examples


Percy Grunwald's Profile Picture

Written by Percy Grunwald

— Last Updated February 22, 2019

What does the Ansible apt_repository module do?

Ansible’s apt_repository module is used to manage repositories for the apt package manager. The most common use case for this module is adding a third party repository, such as Elasticsearch.

Before adding a repository with this module, you will usually need to add a GPG key with the apt_key module as shown in the examples below.

Superuser (root) privileges are usually required to manage apt keys and repositories, so become: true should be used in most cases.

The apt_repository module is generally used in combination with the apt module and apt_key module:

- name: import the elasticsearch apt key
  apt_key:
    url: https://artifacts.elastic.co/GPG-KEY-elasticsearch
    state: present
  become: true

- name: install elasticsearch 6.x deb repository
  apt_repository:
    repo: deb https://artifacts.elastic.co/packages/6.x/apt stable main
    state: present
  become: true

- name: install elasticsearch 6.x
  apt:
    name: "{{ item }}"
    state: present
    update_cache: true
  loop:
    - openjdk-8-jre-headless
    - elasticsearch
  become: true

This module is only relevant for Debian-based Linux distributions such as Debian and Ubuntu. For Red Hat-based distros such as Red Hat Enterprise Linux and CentOS, use the corresponding yum modules to manage keys and repositories, and install packages:

Examples

How to add an apt repository (+ apt key) and install a package

The example below adapts the installation instructions for installing Elasticsearch with apt into Ansible form.

How to import an apt GPG key

Packages from Elastic are signed with their GPG key, so we should import the key with the apt_key module:

- name: import the elasticsearch apt key
  apt_key:
    url: https://artifacts.elastic.co/GPG-KEY-elasticsearch
    state: present
  become: true

How to add an apt repository

The instructions call for creating a file in /etc/apt/sources.list.d/; this is what the apt_repository module will do automatically. Set the repo parameter to the repo string, which is usually in the form deb {{ repo_url }} stable main.

- name: install elasticsearch 6.x deb repository
  apt_repository:
    repo: deb https://artifacts.elastic.co/packages/6.x/apt stable main
    state: present
  become: true

How to install Elasticsearch 6.x (+ Java)

Now that the elasticsearch-6.x repo has been added, we can install the elasticsearch package with the apt module. apt will search all available repositories to look for the elasticsearch package and install it if not already installed.

Java is a prerequisite for elasticsearch and must be fully installed before attempting to install elasticsearch. Using the loop keyword rather than passing the list to name means that the command below will install openjdk-8-jre-headless before attempting to install elasticsearch:

- name: install elasticsearch 6.x
  apt:
    name: "{{ item }}"
    state: present
    update_cache: true
  loop:
    - openjdk-8-jre-headless
    - elasticsearch
  become: true

How to remove an apt repository

Removing an apt repository is as simple as setting the repo parameter and setting state: absent.

- name: remove elasticsearch 6.x deb repository
  apt_repository:
    repo: deb https://artifacts.elastic.co/packages/6.x/apt stable main
    state: absent
  become: true

How to capture apt_repository module output

Use the register keyword to capture the output of the apt_repository module.

- name: install elasticsearch 6.x deb repository
  apt_repository:
    repo: deb https://artifacts.elastic.co/packages/6.x/apt stable main
    state: present
  become: true
  register: apt_repository_output

The debug task above will output the following:

ok: [123.123.123.123] => {
    "apt_repository_output": {
        "changed": true,
        "diff": {},
        "failed": false,
        "repo": "deb https://artifacts.elastic.co/packages/6.x/apt stable main",
        "state": "present"
    }
}

Further reading

Comment & Share