This role manages APT repositories on Debian-based systems by replacing the default sources.list, adding custom repositories, and managing GPG keys.
Find a file
2025-08-16 12:56:37 +02:00
defaults Rewrite: refactor structure and improve repository/key handling 2025-07-04 10:18:04 +02:00
handlers Standardize YAML file formatting 2025-06-06 16:23:58 +02:00
meta add namespace 2025-06-21 11:33:24 +02:00
tasks drop sources backup 2025-08-16 12:56:37 +02:00
templates/sources.list.d update to deb822 2025-08-10 12:57:58 +02:00
.gitignore add namespace 2025-06-21 11:33:24 +02:00
LICENSE Initial commit 2025-06-02 22:03:33 +02:00
README.md add backup cleanup 2025-07-04 10:30:09 +02:00

manage_repositories Role

This role manages APT repositories on Debian-based systems by replacing the default sources.list, adding custom repositories, and managing GPG keys.

Description

The manage_repositories role provides a modular way to manage APT repositories. It allows you to replace the default sources.list, validate repository definitions, add or remove repositories, and manage GPG keys for secure package installation. The role is designed to be flexible and reusable for various repository management tasks.

Installation

  1. Ensure that Ansible is installed on your system. The recommended version is at least 2.17.
  2. Clone this repository or add the role to your existing project.
  3. Ensure that the target system meets the following requirements:
    • Debian-based operating system (e.g., Ubuntu, Debian)
    • Access to the internet for downloading GPG keys and repository metadata

Variables

The role uses the following variables:

  • manage_repositories_mirror_url The base URL for the main Debian package mirror.
    Example: http://ftp.de.debian.org
  • manage_repositories_security_url The base URL for the Debian security updates repository.
    Example: http://security.debian.org
  • manage_repositories_distribution The Debian distribution codename to use (e.g., bookworm, bullseye).
    Example: bookworm
  • manage_repositories_components Space-separated list of Debian repository components to include.
    Default: contrib non-free non-free-firmware
  • manage_repositories_backports Whether to add the backports repository for the specified distribution.
    Default: false
  • manage_repositories_repos
    A list of repositories to be managed. Each repository should include the following keys:
    • name: The name of the repository (used for the .list file).
    • keyrings_url: URL to the GPG key for the repository.
    • keyrings_format: Format of the GPG key, either ascii or binary.
    • keyrings_file: Destination filename for the GPG key (placed in /etc/apt/keyrings/).
    • repo_url: The repository's full URL and distribution/component string (e.g., https://deb.nodesource.com/node_20.x nodistro main).
    • state (optional): Whether the repository should be present or absent.
      Default: present
    • update_cache (optional): Whether to update the APT cache after adding the repository.
      Default: true
    • recreate_source (optional): Whether to remove and recreate the existing repository file.
      Default: false

Usage

Example Playbook

- name: Update sources.list
  ansible.builtin.include_role:
    name: manage_repositories
  vars:
    manage_repositories_mirror_url: 'http://ftp.de.debian.org'
    manage_repositories_security_url: 'http://security.debian.org'
    manage_repositories_distribution: 'bookworm'
    manage_repositories_components: 'contrib non-free non-free-firmware'
    manage_repositories_backports: true

- name: Update repo
  ansible.builtin.include_role:
    name: manage_repositories
  vars:
    manage_repositories_repos:
      - name: nodejs
        keyrings_url: 'https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key'
        keyrings_format: 'ascii'
        keyrings_file: 'nodesource.gpg'
        repo_url: 'https://deb.nodesource.com/node_20.x nodistro main'
        state: present
        update_cache: true
        recreate_source: false

Features

  • Replace sources.list: Replaces the default APT sources list with a custom template.
  • Validate Repository Definitions: Ensures that all required variables for repositories are defined.
  • Add or Remove Repositories: Dynamically manages repository files based on the provided configuration.
  • Manage GPG Keys: Downloads and installs GPG keys for secure repository access.
  • APT Cache Management: Updates the APT cache when necessary.
  • Modular Design: Tasks are split into logical components for better maintainability.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your changes.
  3. Submit a pull request describing your changes.

License

This project is licensed under the MIT License. For more information, see the LICENSE file.