Ansible 模块 —— 设置软件仓库和安装软件包

设置软件仓库

ansible.builtin.rpm_key

ansible.builtin.rpm_key 用于在 Fedora/RHEL 上导入或移除 GPG 公钥

参数名 类型 默认值 说明
fingerprint str null 指定公钥的完整指纹(long-form)。在导入前会比对公钥是否匹配此指纹,增强安全性。建议用于确保下载的 key 没有被篡改。例:6A2FAEA2352C64E5ED8D8A43A1F59849F0E6AF8D
key str null 公钥路径或 URL,或已存在的 key ID。可用来导入(本地或远程)或删除(用 keyid)。
state str present 指定状态:
present 导入 GPG key
absent 移除 GPG key
validate_certs bool true 下载远程 key 时,是否验证 HTTPS 证书。 设为 no 可跳过 TLS 验证(不推荐,除非私有站点无证书)。
- name: Import a key from a url
  ansible.builtin.rpm_key:
    state: present
    key: http://apt.sw.be/RPM-GPG-KEY.dag.txt

- name: Import a key from a file
  ansible.builtin.rpm_key:
    state: present
    key: /path/to/key.gpg

- name: Ensure a key is not present in the db
  ansible.builtin.rpm_key:
    state: absent
    key: DEADB33F

- name: Verify the key, using a fingerprint, before import
  ansible.builtin.rpm_key:
    key: /path/to/RPM-GPG-KEY.dag.txt
    fingerprint: EBC6 E12C 62B1 C734 026B  2122 A20E 5214 6B8D 79E6

ansible.builtin.yum_repository

ansible.builtin.yum_repository 用于设置软件仓库:

选项 类型 默认值 说明
name str null 仓库 ID,唯一标识该仓库(即 [epel1] 这段),YUM/DNF 使用它作为内部标识。
description str null 仓库描述,主要是为了可读性,在 .repo 文件中显示为 name=
baseurl str null 仓库的主地址,用于直接访问软件包。URL 中的 $releasever$basearch 会自动替换为系统对应值。
file str name 相同 指定生成的 .repo 文件名(如:/etc/yum.repos.d/external_repos.repo)。
mirrorlist str null 镜像列表地址,用于从多个镜像中自动选择一个。与 baseurl 不同时使用(两者选其一)。
enabled bool true 是否启用该仓库。yes 表示启用,no 表示禁用(默认通常是启用的)。
gpgcheck bool false 是否启用 GPG 签名校验。yes 为开启验证,no 表示不验证 RPM 包的签名。
state str present 可选值有 presentabsent,用于创建或删除仓库配置。
exclude str/list null 排除的包名,支持通配符,如 kernel*
priority int null 仓库优先级(启用 yum-plugin-priorities 时生效)。数值越小优先级越高。
- name: Add repository
  ansible.builtin.yum_repository:
    name: epel1
    description: EPEL YUM repo
    baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
    file: external_repos
    mirrorlist: http://mirrorlist.repoforge.org/el7/mirrors-rpmforge
    enabled: no
    gpgcheck: no
    state: absent

ansible.builtin.apt_key

ansible.builtin.apt_key 用于在 Debian/Ubuntu 系统上添加或移除 APT GPG 公钥

参数名 类型 默认值 说明
id str null 要添加或删除的 key ID(短 ID 或完整指纹均可)。 删除 key 时推荐使用。
url str null GPG 公钥的 URL,支持 HTTPS。会自动下载并添加。
file str null 公钥文件的本地路径,适用于已预先下载到目标机的公钥。
keyring str null 指定要写入的 keyring 文件路径(通常用于 /etc/apt/trusted.gpg.d/*.gpg)。
state str present present 表示添加 key,absent 表示移除 key。
validate_certs bool yes 使用 url 下载 key 时,是否验证 HTTPS 证书。 设为 no 可跳过验证(用于私有源或无证书)。
keyserver str null 指定公钥服务器,如 hkp://keyserver.ubuntu.com,用于从 keyserver 获取 key。
data str null 直接传入 GPG 公钥内容(字符串形式)。
- name: One way to avoid apt_key once it is removed from your distro, armored keys should use .asc extension, binary should use .gpg
  block:
    - name: somerepo | no apt key
      ansible.builtin.get_url:
        url: https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x36a1d7869245c8950f966e92d8576a8ba88d21e9
        dest: /etc/apt/keyrings/myrepo.asc
        checksum: sha256:bb42f0db45d46bab5f9ec619e1a47360b94c27142e57aa71f7050d08672309e0

    - 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

- name: Add an apt key by id from a keyserver
  ansible.builtin.apt_key:
    keyserver: keyserver.ubuntu.com
    id: 36A1D7869245C8950F966E92D8576A8BA88D21E9

- name: Add an Apt signing key, uses whichever key is at the URL
  ansible.builtin.apt_key:
    url: https://ftp-master.debian.org/keys/archive-key-6.0.asc
    state: present

- name: Add an Apt signing key, will not download if present
  ansible.builtin.apt_key:
    id: 9FED2BCBDCD29CDF762678CBAED4B06F473041FA
    url: https://ftp-master.debian.org/keys/archive-key-6.0.asc
    state: present

- name: Remove a Apt specific signing key, leading 0x is valid
  ansible.builtin.apt_key:
    id: 0x9FED2BCBDCD29CDF762678CBAED4B06F473041FA
    state: absent

ansible.builtin.apt_repository

选项 类型 默认值 说明
repo str null 要添加的源定义,格式类似于 deb http://...ppa:...
state str present 设置为 present 添加源,absent 删除源
filename str null 保存源信息的文件名(位于 /etc/apt/sources.list.d/
update_cache bool false 添加源后是否运行 apt-get update
validate_certs bool true 连接 HTTPS 源时是否验证 SSL 证书
codename str null 指定发行版代号(如 bionic, focal
mode str 0644 保存源文件时的权限
state str present 是否添加或删除源(可为 presentabsent
- name: 添加 MongoDB 源
  ansible.builtin.apt_repository:
    repo: "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse"
    state: present
    filename: "mongodb"
    update_cache: yes

- name: 添加 PPA 源
  ansible.builtin.apt_repository:
    repo: ppa:deadsnakes/ppa
    state: present
    update_cache: yes

- name: 删除 mongodb 源
  ansible.builtin.apt_repository:
    filename: mongodb
    state: absent

软件包安装

ansible.builtin.yum

ansible.builtin.yum 模块兼容旧版本系统(RHEL6/7)

所有选项:

选项 类型 默认值 说明
allow_downgrade bool false 允许安装较旧版本的软件包。
autoremove bool false 删除软件包时自动移除其依赖。
bugfix bool false 仅应用 bugfix 类型更新(需启用插件)。
conf_file str /etc/yum.conf 指定 YUM 配置文件路径。
disable_gpg_check bool false 禁用 GPG 签名验证。
disable_plugin str/list null 禁用指定插件(如 fastestmirror)。
enable_plugin str/list null 启用指定的插件,多个插件可以通过列表形式启用。
disablerepo str/list null 禁用特定仓库。
download_only bool false 仅下载,不安装。
download_dir str null 下载 .rpm 包的保存路径(需配合 download_only 使用)。
enablerepo str/list null 启用特定仓库。
exclude str/list null 排除匹配的包名,支持通配符。
install_weak_deps bool true 是否安装弱依赖(推荐包)。
installroot str / 更改安装根路径,常用于 chroot。
list str null 显示匹配的软件包列表,例如 updatesavailable
lock_timeout int 30 等待 YUM 锁释放的时间(秒)。
name str/list null 包名,支持单个或多个,也支持 *.rpm 路径。
security bool false 仅应用安全更新(需启用插件)。
skip_broken bool false 忽略依赖问题并跳过损坏包。
state str present 包的期望状态:presentlatestabsentremoved
update_cache bool false 先更新 YUM 缓存(等同 yum makecache)。
update_only bool false 仅允许更新已安装的软件包,不做新安装。
validate_certs bool true 是否校验 HTTPS 仓库 SSL 证书。
install_repoquery bool true 自动安装 repoquery 工具(用于判断包状态)。
disable_excludes str null 忽略某些配置中的 exclude=(如 main, all, repoid)。
cacheonly bool false 只使用本地缓存的包,避免从网络下载。
releasever str 当前系统版本号 强制使用指定版本的仓库,适用于不同版本的操作系统。
sslverify bool true 控制是否验证 SSL 证书,适用于不需要验证证书的仓库。
use_backend str null 强制使用指定的包管理后端工具,通常是 yumdnf

常用选项:

选项名 说明 示例
name 软件包名称,可以是单个包或列表,也支持通配符(软件包组需要在前边加 @ nginxhttpd*@Development tools
state 包的期望状态,如 presentlatestabsentremoved state: present
enablerepo 启用指定的 YUM 仓库进行安装 enablerepo: epel
disablerepo 禁用指定仓库 disablerepo: base
update_cache 更新缓存(yum makecache update_cache: true
disable_gpg_check 禁用 GPG 签名检查 disable_gpg_check: true
exclude 排除匹配的包名,支持通配符。 kernel*
download_only 只下载不安装(适合离线环境) download_only: true
download_dir 下载 .rpm 包的保存路径(需配合 download_only 使用,默认在 /var/cache/dnf/)。 download_dir: /tmp/
- name: Install the latest version of Apache
  ansible.builtin.yum:
    name: httpd
    state: latest

- name: Install Apache >= 2.4
  ansible.builtin.yum:
    name: httpd>=2.4
    state: present

- name: Install a list of packages (suitable replacement for 2.11 loop deprecation warning)
  ansible.builtin.yum:
    name:
      - nginx
      - postgresql
      - postgresql-server
    state: present

- name: Remove the Apache package
  ansible.builtin.yum:
    name: httpd
    state: absent

- name: Upgrade all packages, excluding kernel & foo related packages
  ansible.builtin.yum:
    name: '*'
    state: latest
    exclude: kernel*,foo*

- name: Install the nginx rpm from a remote repo
  ansible.builtin.yum:
    name: http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
    state: present

- name: Install nginx rpm from a local file
  ansible.builtin.yum:
    name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
    state: present

- name: Install the 'Development tools' package group
  ansible.builtin.yum:
    name: "@Development tools"
    state: present

- name: Install package with multiple repos enabled
  ansible.builtin.yum:
    name: sos
    enablerepo: "epel,ol7_latest"

- name: Download the nginx package but do not install it
  ansible.builtin.yum:
    name:
      - nginx
    state: latest
    download_only: true
    download_dir: /tmp/

- name: Update cache
  ansible.builtin.yum:
    update_cache: true

ansible.builtin.dnf

ansible.builtin.dnf 模块更适合新版本系统(RHEL 8 以后)

大部分选项和 ansible.builtin.yum 相同,比 yum 多了两个选项:

选项 作用 默认值 示例
allowerasing 允许删除冲突包以解决依赖关系 no allowerasing: yes
nobest 不强制安装最新版本,而是选择合适版本 no nobest: yes

使用方法和 ansible.builtin.yum 相同,模块名改成 ansible.builtin.dnf 就行。

ansible.builtin.dnf5

这个还在开发中,相比于 ansible.builtin.dnf,少了 use_backend 选项。

ansible.builtin.apt

参数 类型 默认值 说明
name str / list null 要安装、删除或管理的软件包名称或列表
state str present 包的状态:presentabsentlatest
update_cache bool false 是否在安装前执行 apt-get update
upgrade str false 升级策略:dist, full, safe, yes, no
autoremove bool false 是否自动删除不再需要的依赖包
purge bool false 删除时是否同时清理配置文件(apt-get purge
force bool false 强制操作(谨慎使用)
only_upgrade bool false 仅升级已安装的软件包
default_release str null 指定默认的 apt 发布版本
dpkg_options list null 传递给 dpkg 的参数,例如:['--force-confold']
deb str null 安装本地 .deb 文件或 URL 地址
- name: Installation of common tools
  ansible.builtin.apt:
    name:
      - vim
      - git
      - curl
    state: present

- name: Update cache and install
  ansible.builtin.apt:
    name: htop
    state: present
    update_cache: yes

- name: Upgrade system
  ansible.builtin.apt:
    upgrade: dist

- name: Uninstall nginx
  ansible.builtin.apt:
    name: nginx
    state: absent
    purge: yes

- name: Install local deb package
  ansible.builtin.apt:
    deb: /tmp/custom-package.deb
Ansible 模块 —— 设置软件仓库和安装软件包
https://www.linuxstudynotes.com/2025/05/06/ansible-%e6%a8%a1%e5%9d%97/ansible-%e6%a8%a1%e5%9d%97-%e8%ae%be%e7%bd%ae%e8%bd%af%e4%bb%b6%e4%bb%93%e5%ba%93%e5%92%8c%e5%ae%89%e8%a3%85%e8%bd%af%e4%bb%b6%e5%8c%85/
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇