Skip to content

apt_repository

Collection Note

This module is part of the ansible.builtin collection. To install the collection, use:

ansible-galaxy collection install ansible.builtin
Added in version 0.7.

You need further requirements to be able to use this module, see the Requirements section for details.

Synopsis

  • Add or remove an APT repositories in Ubuntu and Debian.

Requirements

The following Python packages are needed on the host that executes this module:

Parameters

Parameter Defaults / Choices Comments
codename
str
Override the distribution codename to use for PPA repositories. Should usually only be set when working with a PPA on a non-Ubuntu target (for example, Debian or Mint).
Version Added: 2.3
filename
str
Sets the name of the source list file in C(sources.list.d). Defaults to a file name based on the repository source url. The C(.list) extension will be automatically added.
Version Added: 2.1
install_python_apt
bool
Default: True
Whether to automatically try to install the Python apt library or not, if it is not already installed. Without this library, the module does not work.
Runs C(apt-get install python3-apt).
Only works with the system Python. If you are using a Python on the remote that is not the system Python, set O(install_python_apt=false) and ensure that the Python apt library for your Python version is installed some other way.
mode
raw
The octal mode for newly created files in C(sources.list.d).
Default is what system uses (probably 0644).
Version Added: 1.6
repo
str
required
A source string for the repository.
state
str
Default: present
Choices: absent, present
A source string state.
update_cache
bool
Default: yes
Run the equivalent of C(apt-get update) when a change occurs. Cache updates are run after making changes.
update_cache_retries
int
Default: 5
Amount of retries if the cache update fails. Also see O(update_cache_retry_max_delay).
Version Added: 2.10
update_cache_retry_max_delay
int
Default: 12
Use an exponential backoff delay for each retry (see O(update_cache_retries)) up to this max delay in seconds.
Version Added: 2.10
validate_certs
bool
Default: yes
If V(false), SSL certificates for the target repo will not be validated. This should only be used on personally controlled sites using self-signed certificates.
Version Added: 1.8

Notes

Note

  • This module supports Debian Squeeze (version 6) as well as its successors and derivatives.

Examples

- name: Add specified repository into sources list
  ansible.builtin.apt_repository:
    repo: deb http://archive.canonical.com/ubuntu hardy partner
    state: present

- name: Add specified repository into sources list using specified filename
  ansible.builtin.apt_repository:
    repo: deb http://dl.google.com/linux/chrome/deb/ stable main
    state: present
    filename: google-chrome

- name: Add source repository into sources list
  ansible.builtin.apt_repository:
    repo: deb-src http://archive.canonical.com/ubuntu hardy partner
    state: present

- name: Remove specified repository from sources list
  ansible.builtin.apt_repository:
    repo: deb http://archive.canonical.com/ubuntu hardy partner
    state: absent

- name: Add nginx stable repository from PPA and install its signing key on Ubuntu target
  ansible.builtin.apt_repository:
    repo: ppa:nginx/stable

- name: Add nginx stable repository from PPA and install its signing key on Debian target
  ansible.builtin.apt_repository:
    repo: 'ppa:nginx/stable'
    codename: trusty

- name: One way to avoid apt_key once it is removed from your distro
  block:
    - name: somerepo |no apt key
      ansible.builtin.get_url:
        url: https://download.example.com/linux/ubuntu/gpg
        dest: /etc/apt/keyrings/somerepo.asc

    - name: somerepo | apt source
      ansible.builtin.apt_repository:
        repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/somerepo.asc] https://download.example.com/linux/ubuntu {{ ansible_distribution_release }} stable"
        state: present

Return Values

Key Data Type Description Returned
repo str A source string for the repository always
sources_added list List of sources added success, sources were added
sources_removed list List of sources removed success, sources were removed

Authors

  • Alexander Saltanov (@sashka)