ansible.builtin.apt_repository 模块 – 添加和删除 APT 仓库

注意

此模块是 ansible-core 的一部分,包含在所有 Ansible 安装中。在大多数情况下,即使没有指定 collections 关键字,也可以使用简短的模块名称 apt_repository。但是,我们建议您使用 完全限定的集合名称 (FQCN) ansible.builtin.apt_repository,以便轻松链接到模块文档并避免与可能具有相同模块名称的其他集合冲突。

概要

  • 在 Ubuntu 和 Debian 中添加或删除 APT 仓库。

需求

以下需求在执行此模块的主机上是必需的。

  • python-apt(python 2)

  • python3-apt(python 3)

  • apt-key 或 gpg

参数

参数

注释

codename

字符串

覆盖用于 PPA 仓库的发行版代号。通常只应在针对非 Ubuntu 目标(例如 Debian 或 Mint)上的 PPA 时设置。

filename

字符串

设置 sources.list.d 中源列表文件的名称。默认为基于仓库源 URL 的文件名。 .list 扩展名将自动添加。

install_python_apt

布尔值

是否自动尝试安装 Python apt 库,如果它尚未安装。如果没有此库,该模块将无法工作。

运行 apt-get install python-apt 用于 Python 2,以及 apt-get install python3-apt 用于 Python 3。

仅适用于系统 Python 2 或 Python 3。如果您使用的是远程主机上的非系统 Python,请设置 install_python_apt=false 并确保您的 Python 版本的 Python apt 库已通过其他方式安装。

选项

  • false

  • true ← (默认)

mode

任何

sources.list.d 中新创建的文件的八进制模式。

默认值是系统使用的值(可能是 0644)。

repo

字符串 / 必需

仓库的源字符串。

state

字符串

源字符串状态。

选项

  • "absent"

  • "present" ← (默认)

update_cache

别名:update-cache

布尔值

在发生更改时运行等效于 apt-get update 的命令。缓存更新在进行更改后运行。

选项

  • false

  • true ← (默认)

update_cache_retries

整数

在 ansible-base 2.10 中添加

如果缓存更新失败,则重试次数。另请参阅 update_cache_retry_max_delay

默认值: 5

update_cache_retry_max_delay

整数

在 ansible-base 2.10 中添加

对每次重试使用指数退避延迟(见 update_cache_retries),直到此最大延迟(以秒为单位)。

默认值: 12

validate_certs

布尔值

如果为 false,则不会验证目标仓库的 SSL 证书。这应该只在使用自签名证书的个人控制的站点上使用。

选项

  • false

  • true ← (默认)

属性

属性

支持

描述

check_mode

支持:完全支持

可以在 check_mode 中运行,并在不修改目标的情况下返回更改状态预测,如果不受支持,则将跳过该操作。

diff_mode

支持:完全支持

将在差异模式下返回有关更改内容(或可能需要在 check_mode 中更改的内容)的详细信息

platform

平台: debian

可以针对其进行操作的目标操作系统/系列

注释

注意

  • 此模块支持 Debian Squeeze(版本 6)及其后续版本和衍生版本。

另请参阅

另请参阅

ansible.builtin.deb822_repository

添加和删除 deb822 格式的仓库。

示例

- 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/myrepo.asc] https://download.example.com/linux/ubuntu {{ ansible_distribution_release }} stable"
        state: present

返回值

常见的返回值已记录在 此处,以下是此模块特有的字段

Key

描述

repo

字符串

仓库的源字符串

返回值:始终

示例: "deb https://artifacts.elastic.co/packages/6.x/apt stable main"

sources_added

列表 / 元素=字符串

在 ansible-core 2.15 中添加

已添加的源列表

返回值:成功,源已添加

示例: ["/etc/apt/sources.list.d/artifacts_elastic_co_packages_6_x_apt.list"]

sources_removed

列表 / 元素=字符串

在 ansible-core 2.15 中添加

已删除的源列表

返回值:成功,源已删除

示例: ["/etc/apt/sources.list.d/artifacts_elastic_co_packages_6_x_apt.list"]

作者

  • Alexander Saltanov (@sashka)