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 
参数
| 参数 | 注释 | 
|---|---|
| 覆盖用于 PPA 仓库的发行版代号。通常只有在非 Ubuntu 目标(例如 Debian 或 Mint)上使用 PPA 时才需要设置。 | |
| 设置  | |
| 如果 Python apt 库尚未安装,是否自动尝试安装它。没有这个库,模块无法工作。 对于 Python 2,运行  仅适用于系统 Python 2 或 Python 3。如果您使用的是远程主机上非系统 Python,请设置  选项 
 | |
| 
 默认为系统使用的模式(可能是 0644)。 | |
| 仓库的源字符串。 | |
| 源字符串状态。 选项 
 | |
| 发生更改时运行等效于  选项 
 | |
| 如果缓存更新失败,则重试次数。另请参见  默认值:  | |
| 对每次重试使用指数退避延迟(参见  默认值:  | |
| 如果为  选项 
 | 
属性
| 属性 | 支持 | 描述 | 
|---|---|---|
| 支持:完全支持 | 可以在 check_mode 下运行并返回更改状态预测,而无需修改目标,如果不支持,则操作将被跳过。 | |
| 支持:完全支持 | 在 diff 模式下,将返回有关更改内容(或可能在 check_mode 下需要更改的内容)的详细信息 | |
| 平台: 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/somerepo.asc] https://download.example.com/linux/ubuntu {{ ansible_distribution_release }} stable"
        state: present
返回值
常见返回值已在此处 记录,以下是此模块特有的字段
| 键 | 描述 | 
|---|---|
| 仓库的源字符串 返回:始终 示例:  | |
| 已添加的源列表 返回:成功,已添加源 示例:  | |
| 已删除的源列表 返回:成功,已删除源 示例:  | 
