ansible.builtin.apt 模块 - 管理 apt 包

注意

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

概要

  • 管理 apt 包(例如,用于 Debian/Ubuntu)。

要求

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

  • python-apt(python 2)

  • python3-apt(python 3)

  • aptitude(2.4 之前的版本)

参数

参数

注释

allow_change_held_packages

布尔值

在 ansible-core 2.13 中添加

允许更改位于 apt 保留列表中的包的版本。

选项

  • false ← (默认)

  • true

allow_downgrade

别名:allow-downgrade、allow_downgrades、allow-downgrades

布尔值

在 ansible-core 2.12 中添加

对应于 apt--allow-downgrades 选项。

此选项允许指定的包和版本替换已安装的更高版本的该包。

请注意,设置 allow_downgrade=true 会使此模块的行为变得非幂等。

(任务可能最终会得到一组与指定安装的完整包列表不匹配的包。)

allow_downgrade 仅受 apt 支持,如果检测到或指定了 aptitude,它将被忽略。

选项

  • false ← (默认)

  • true

allow_unauthenticated

别名:allow-unauthenticated

布尔值

如果无法验证包,则忽略。这对引导管理自己的 apt 密钥设置的环境很有用。

allow_unauthenticated 仅支持 stateinstall/present

选项

  • false ← (默认)

  • true

autoclean

布尔值

如果为 true,则清理本地存储库中无法再下载的已检索包文件。

选项

  • false ← (默认)

  • true

autoremove

布尔值

如果为 true,则删除除 build-dep 之外的所有模块状态的未使用依赖包。它也可以用作唯一的选项。

在 2.4 版之前,autoclean 也是 autoremove 的别名,现在它是一个独立的命令。有关更多信息,请参阅文档。

选项

  • false ← (默认)

  • true

cache_valid_time

整数

如果 apt 缓存比 cache_valid_time 旧,则更新 apt 缓存。此选项以秒为单位设置。

从 Ansible 2.4 开始,如果显式设置,则这将设置 update_cache=yes

默认: 0

clean

布尔值

在 ansible-core 2.13 中添加

运行相当于 apt-get clean 的操作,以清除本地存储库中的已检索包文件。它从 /var/cache/apt/archives//var/cache/apt/archives/partial/ 中删除所有内容,但锁定文件除外。

可以作为包安装的一部分运行(clean 在安装之前运行),也可以作为单独的步骤运行。

选项

  • false ← (默认)

  • true

deb

路径

远程机器上 .deb 包的路径。

如果路径中包含 ://,ansible 将尝试在安装之前下载 deb。(版本 2.1 中添加)

需要 xz-utils 包来提取 deb 包的控制文件以进行安装。

default_release

别名:default-release

字符串

对应于 apt-t 选项,并设置引脚优先级。

dpkg_options

字符串

apt 命令添加 dpkg 选项。默认为 -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold"

选项应作为逗号分隔列表提供。

默认: "force-confdef,force-confold"

fail_on_autoremove

布尔值

在 ansible-core 2.11 中添加

对应于 --no-removeapt 选项。

如果为 true,则确保不会删除任何包或任务将失败。

fail_on_autoremove 仅支持 state,但不包括 absent

fail_on_autoremove 仅受 apt 支持,如果检测到或指定了 aptitude,它将被忽略。

选项

  • false ← (默认)

  • true

force

布尔值

对应于 --force-yesapt-get,并隐含 allow_unauthenticated=yesallow_downgrade=yes

此选项将禁用检查包的签名和从其下载它们的 Web 服务器的证书。

此选项*不*等效于在命令行上向 apt-get 传递 -f 标志。

**这是一项可能破坏您的系统的破坏性操作,几乎不应该使用。** 请参阅 man apt-get 以获取更多信息。

选项

  • false ← (默认)

  • true

force_apt_get

布尔值

强制使用 apt-get 而不是 aptitude。

选项

  • false ← (默认)

  • true

install_recommends

别名:install-recommends

布尔值

对应于 --no-install-recommendsapt 选项。true 安装推荐的包。 false 不会安装推荐的包。默认情况下,Ansible 将使用与操作系统相同的默认值。建议的包永远不会安装。

选项

  • false

  • true

lock_timeout

整数

在 ansible-core 2.12 中添加

此操作将等待多少秒来获取 apt 数据库的锁定。

有时会存在瞬态锁定,这将至少重试到超时为止。

默认: 60

name

别名:package、pkg

列表 / 元素=字符串

一个包名列表,例如 foo,或带有版本的包说明符,例如 foo=1.0foo>=1.0。名称通配符(fnmatch)如 apt* 和版本通配符如 foo=1.0* 也受支持。

当使用特定版本的包名时,不要在版本周围使用单引号或双引号,例如 foo=1.0foo>=1.0

only_upgrade

布尔值

仅在包已安装的情况下升级包。

选项

  • false ← (默认)

  • true

policy_rc_d

整数

在 Ansible 2.8 中添加

强制 /usr/sbin/policy-rc.d 的退出代码。

例如,如果 policy_rc_d=101,已安装的包将不会触发服务启动。

如果 /usr/sbin/policy-rc.d 已经存在,它将在包安装后被备份并恢复。

如果为 null,则不会创建/更改 /usr/sbin/policy-rc.d

purge

布尔值

如果 state=absentautoremove=yes,将强制清除配置文件。

选项

  • false ← (默认)

  • true

state

字符串

指示所需的包状态。 latest 确保安装最新版本。 build-dep 确保安装包构建依赖项。 fixed 尝试纠正具有损坏依赖项的系统。

选项

  • "absent"

  • "build-dep"

  • "latest"

  • "present" ← (默认)

  • "fixed"

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

upgrade

字符串

如果为 yes 或 safe,则执行 aptitude 安全升级。

如果为 full,则执行 aptitude 全面升级。

如果为 dist,则执行 apt-get dist-upgrade。

注意:这不会升级特定包,请使用 state=latest 来执行该操作。

注意:从 2.4 版开始,如果 aptitude 不存在,则使用 apt-get 作为备用。

选项

  • "dist"

  • "full"

  • "no" ← (默认)

  • "safe"

  • "yes"

属性

属性

支持

描述

check_mode

支持:完整

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

diff_mode

支持:完整

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

platform

平台: debian

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

注意

注意

  • 三种升级模式(fullsafe 及其别名 true)在 2.3 版之前需要 aptitude,从 2.4 版开始使用 apt-get 作为备用。

  • 在大多数情况下,使用 apt 安装的包默认情况下会启动新安装的服务。大多数发行版都有机制来避免这种情况。例如,在 Debian 9 中安装 Postgresql-9.5 时,创建一个可执行的 shell 脚本(/usr/sbin/policy-rc.d),该脚本抛出一个返回码 101,将阻止 Postgresql 9.5 在安装后启动。之后删除该文件或其执行权限。

  • apt-get 命令行支持隐式正则表达式匹配,但我们不支持,因为它更容易让错别字通过(如果您将 foo 误写为 fo,apt-get 会安装名称中包含“fo”的包,并显示警告和用户提示。由于在安装之前没有警告和提示,因此我们不允许这样做。如果您想要通配符匹配,请使用显式 fnmatch 模式)。

  • 当与 loop: 一起使用时,每个包将分别处理,将列表直接传递给 name 选项效率更高。

  • 当使用 default_release 时,将使用 990 的隐式优先级。这与 apt-get -t 的行为相同。

  • 当指定确切版本时,将使用 1001 的隐式优先级。

  • 如果解释器无法导入 python-apt/python3-apt,模块将在系统拥有的解释器中检查它。如果找不到依赖项,模块将尝试安装它。如果找到或安装了依赖项,模块将在正确的解释器下重新生成。

示例

- name: Install apache httpd (state=present is optional)
  ansible.builtin.apt:
    name: apache2
    state: present

- name: Update repositories cache and install "foo" package
  ansible.builtin.apt:
    name: foo
    update_cache: yes

- name: Remove "foo" package
  ansible.builtin.apt:
    name: foo
    state: absent

- name: Install the package "foo"
  ansible.builtin.apt:
    name: foo

- name: Install a list of packages
  ansible.builtin.apt:
    pkg:
    - foo
    - foo-tools

- name: Install the version '1.00' of package "foo"
  ansible.builtin.apt:
    name: foo=1.00

- name: Update the repository cache and update package "nginx" to latest version using default release squeeze-backport
  ansible.builtin.apt:
    name: nginx
    state: latest
    default_release: squeeze-backports
    update_cache: yes

- name: Install the version '1.18.0' of package "nginx" and allow potential downgrades
  ansible.builtin.apt:
    name: nginx=1.18.0
    state: present
    allow_downgrade: yes

- name: Install zfsutils-linux with ensuring conflicted packages (e.g. zfs-fuse) will not be removed.
  ansible.builtin.apt:
    name: zfsutils-linux
    state: latest
    fail_on_autoremove: yes

- name: Install latest version of "openjdk-6-jdk" ignoring "install-recommends"
  ansible.builtin.apt:
    name: openjdk-6-jdk
    state: latest
    install_recommends: no

- name: Update all packages to their latest version
  ansible.builtin.apt:
    name: "*"
    state: latest

- name: Upgrade the OS (apt-get dist-upgrade)
  ansible.builtin.apt:
    upgrade: dist

- name: Run the equivalent of "apt-get update" as a separate step
  ansible.builtin.apt:
    update_cache: yes

- name: Only run "update_cache=yes" if the last one is more than 3600 seconds ago
  ansible.builtin.apt:
    update_cache: yes
    cache_valid_time: 3600

- name: Pass options to dpkg on run
  ansible.builtin.apt:
    upgrade: dist
    update_cache: yes
    dpkg_options: 'force-confold,force-confdef'

- name: Install a .deb package
  ansible.builtin.apt:
    deb: /tmp/mypackage.deb

- name: Install the build dependencies for package "foo"
  ansible.builtin.apt:
    pkg: foo
    state: build-dep

- name: Install a .deb package from the internet
  ansible.builtin.apt:
    deb: https://example.com/python-ppq_0.1-1_all.deb

- name: Remove useless packages from the cache
  ansible.builtin.apt:
    autoclean: yes

- name: Remove dependencies that are no longer required
  ansible.builtin.apt:
    autoremove: yes

- name: Remove dependencies that are no longer required and purge their configuration files
  ansible.builtin.apt:
    autoremove: yes
    purge: true

- name: Run the equivalent of "apt-get clean" as a separate step
  ansible.builtin.apt:
    clean: yes

返回值

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

描述

cache_update_time

整数

上次缓存更新的时间(如果未知则为 0)

返回:成功,在某些情况下

示例: 1425828348000

cache_updated

布尔值

缓存是否已更新

返回:成功,在某些情况下

示例: true

stderr

字符串

来自 apt 的错误输出

返回:成功,在需要时

示例: "AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to ..."

stdout

字符串

来自 apt 的输出

返回:成功,在需要时

示例: "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following extra packages will be installed:\n  apache2-bin ..."

作者

  • Matthew Williams (@mgwilliams)