当前位置: 首页 > news >正文

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

设置软件仓库

ansible.builtin.rpm_key

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

参数名

类型

默认值

说明

fingerprintstrnull

指定公钥的完整指纹(long-form)。在导入前会比对公钥是否匹配此指纹,增强安全性。建议用于确保下载的 key 没有被篡改。例:6A2FAEA2352C64E5ED8D8A43A1F59849F0E6AF8D

keystrnull

公钥路径或 URL,或已存在的 key ID。可用来导入(本地或远程)或删除(用 keyid)。

statestrpresent

指定状态:
• present 导入 GPG key
• absent 移除 GPG key

validate_certsbooltrue

下载远程 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 用于设置软件仓库:

选项

类型

默认值

说明

namestrnull

仓库 ID,唯一标识该仓库(即 [epel1] 这段),YUM/DNF 使用它作为内部标识。

descriptionstrnull

仓库描述,主要是为了可读性,在 .repo 文件中显示为 name=

baseurlstrnull

仓库的主地址,用于直接访问软件包。URL 中的 $releasever 和 $basearch 会自动替换为系统对应值。

filestr

name 相同

指定生成的 .repo 文件名(如:/etc/yum.repos.d/external_repos.repo)。

mirrorliststrnull

镜像列表地址,用于从多个镜像中自动选择一个。与 baseurl 不同时使用(两者选其一)。

enabledbooltrue

是否启用该仓库。yes 表示启用,no 表示禁用(默认通常是启用的)。

gpgcheckboolfalse

是否启用 GPG 签名校验。yes 为开启验证,no 表示不验证 RPM 包的签名。

statestrpresent

可选值有 presentabsent,用于创建或删除仓库配置。

excludestr/listnull

排除的包名,支持通配符,如 kernel*

priorityintnull

仓库优先级(启用 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 公钥

参数名

类型

默认值

说明

idstrnull

要添加或删除的 key ID(短 ID 或完整指纹均可)。 删除 key 时推荐使用。

urlstrnull

GPG 公钥的 URL,支持 HTTPS。会自动下载并添加。

filestrnull

公钥文件的本地路径,适用于已预先下载到目标机的公钥。

keyringstrnull

指定要写入的 keyring 文件路径(通常用于 /etc/apt/trusted.gpg.d/*.gpg)。

statestrpresentpresent

 表示添加 key,absent 表示移除 key。

validate_certsboolyes

使用 url 下载 key 时,是否验证 HTTPS 证书。 设为 no 可跳过验证(用于私有源或无证书)。

keyserverstrnull

指定公钥服务器,如 hkp://keyserver.ubuntu.com,用于从 keyserver 获取 key。

datastrnull

直接传入 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

选项

类型

默认值

说明

repostrnull

要添加的源定义,格式类似于 deb http://... 或 ppa:...

statestrpresent

设置为 present 添加源,absent 删除源

filenamestrnull

保存源信息的文件名(位于 /etc/apt/sources.list.d/

update_cacheboolfalse

添加源后是否运行 apt-get update

validate_certsbooltrue

连接 HTTPS 源时是否验证 SSL 证书

codenamestrnull

指定发行版代号(如 bionicfocal

modestr0644

保存源文件时的权限

statestrpresent

是否添加或删除源(可为 present 或 absent

- 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_downgradeboolfalse

允许安装较旧版本的软件包。

autoremoveboolfalse

删除软件包时自动移除其依赖。

bugfixboolfalse

仅应用 bugfix 类型更新(需启用插件)。

conf_filestr/etc/yum.conf

指定 YUM 配置文件路径。

disable_gpg_checkboolfalse

禁用 GPG 签名验证。

disable_pluginstr/listnull

禁用指定插件(如 fastestmirror)。

enable_pluginstr/listnull

启用指定的插件,多个插件可以通过列表形式启用。

disablerepostr/listnull

禁用特定仓库。

download_onlyboolfalse

仅下载,不安装。

download_dirstrnull

下载 .rpm 包的保存路径(需配合 download_only 使用)。

enablerepostr/listnull

启用特定仓库。

excludestr/listnull

排除匹配的包名,支持通配符。

install_weak_depsbooltrue

是否安装弱依赖(推荐包)。

installrootstr/

更改安装根路径,常用于 chroot。

liststrnull

显示匹配的软件包列表,例如 updatesavailable

lock_timeoutint30

等待 YUM 锁释放的时间(秒)。

namestr/listnull

包名,支持单个或多个,也支持 *.rpm 路径。

securityboolfalse

仅应用安全更新(需启用插件)。

skip_brokenboolfalse

忽略依赖问题并跳过损坏包。

statestrpresent

包的期望状态:presentlatestabsentremoved

update_cacheboolfalse

先更新 YUM 缓存(等同 yum makecache)。

update_onlyboolfalse

仅允许更新已安装的软件包,不做新安装。

validate_certsbooltrue

是否校验 HTTPS 仓库 SSL 证书。

install_repoquerybooltrue

自动安装 repoquery 工具(用于判断包状态)。

disable_excludesstrnull

忽略某些配置中的 exclude=(如 mainallrepoid)。

cacheonlyboolfalse

只使用本地缓存的包,避免从网络下载。

releaseverstr

当前系统版本号

强制使用指定版本的仓库,适用于不同版本的操作系统。

sslverifybooltrue

控制是否验证 SSL 证书,适用于不需要验证证书的仓库。

use_backendstrnull

强制使用指定的包管理后端工具,通常是 yum 或 dnf

常用选项:

选项名

说明

示例

name

软件包名称,可以是单个包或列表,也支持通配符(软件包组需要在前边加 @

nginx

 或 httpd* 或 @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

允许删除冲突包以解决依赖关系

noallowerasing: yes
nobest

不强制安装最新版本,而是选择合适版本

nonobest: yes

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

ansible.builtin.dnf5

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

ansible.builtin.apt

参数

类型

默认值

说明

namestr

 / list

null

要安装、删除或管理的软件包名称或列表

statestrpresent

包的状态:presentabsentlatest

update_cacheboolfalse

是否在安装前执行 apt-get update

upgradestrfalse

升级策略:distfullsafeyesno

autoremoveboolfalse

是否自动删除不再需要的依赖包

purgeboolfalse

删除时是否同时清理配置文件(apt-get purge

forceboolfalse

强制操作(谨慎使用)

only_upgradeboolfalse

仅升级已安装的软件包

default_releasestrnull

指定默认的 apt 发布版本

dpkg_optionslistnull

传递给 dpkg 的参数,例如:['--force-confold']

debstrnull

安装本地 .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

http://www.xdnf.cn/news/496927.html

相关文章:

  • Python海龟绘图(Turtle Graphics)核心函数和关键要点
  • 【Linux网络】内网穿透
  • 当语言模型学会犯错和改正:搜索流(SoS)方法解析
  • 兰亭妙微:用系统化思维重构智能座舱 UI 体验
  • 【Redis】零碎知识点(易忘 / 易错)总结回顾
  • linux标准库头文件解析
  • Go语言实现链式调用
  • vscode用python开发maya联动调试设置
  • 游戏引擎学习第288天:继续完成Brains
  • 98. 验证二叉搜索树
  • 信息系统项目管理师高级-软考高项案例分析备考指南(2023年案例分析)
  • 神经网络与深度学习第六章--循环神经网络(理论)
  • WebXR教学 07 项目5 贪吃蛇小游戏
  • 亲测有效!OGG 创建抽取进程报错 OGG-08241,如何解决?
  • 简单神经网络(ANN)实现:从零开始构建第一个模型
  • 【第二篇】 初步解析Spring Boot
  • 第9讲、深入理解Scaled Dot-Product Attention
  • 【漫话机器学习系列】264.内距(又称四分位差)Interquartile Range
  • 抽奖系统-抽奖
  • uni-app小程序登录后…
  • 数据分析_Python
  • arduino平台读取鼠标光电传感器
  • MATLAB学习笔记(七):MATLAB建模城市的雨季防洪排污的问题
  • Elasticsearch 性能优化面试宝典
  • LabVIEW声音与振动测量分析
  • STM32实战指南:SG90舵机控制原理与代码详解
  • Qt与Hid设备通信
  • 392. Is Subsequence
  • 天拓四方锂电池卷绕机 PLC 物联网解决方案
  • 从零开始认识 Node.js:异步非阻塞的魅力