ansible dnf模块详解

本文详细介绍了如何使用 Ansible 的 DNF 模块进行软件包的安装、升级、移除和查看操作。内容包括:指定数据包名称、设置软件包状态、禁用 GPG 验证以及管理仓库等。并提供了多个实际操作示例,涵盖从安装最新版软件到使用特定源安装、卸载软件以及更新所有软件等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

Installs, upgrade, removes, and lists packages and groups with the `dnf’ package manager
通过dnf管理数据包,比如安装、更新、卸载和查看数据包

常用模块

name : A package name or package specifier with version, like `name-1.0’. When using state=latest, this can be '’ which means run: dnf -y update. You can also pass a url or a local path to a rpm file.To operate on several packages this can accept a comma separated string of packages or a list of packages.
(Aliases: pkg)
指定需要安装的数据包名称,星号
代表所有数据包

state:Whether to install (present’, latest’), or remove (absent’) a package.Default is None’, however in effect the default action is present’ unless the autoremove’ option is enabled for this module, then absent’ is inferred.
(Choices: absent, present, installed, removed, latest)[Default: (null)]
指定软件包的状态,installed、present和latest代表安装,remove和absent代表删除

disable_gpg_check :Whether to disable the GPG checking of signatures of packages being installed. Has an effect only if state is present’ or latest’.
[Default: no]
type: bool
用于禁用对 rpm 包的公钥 gpg 验证

enablerepo : `Repoid’ of repositories to enable for the install/update operation. These repos will not persist beyond the transaction. When specifying multiple repos, separate them with a “,”.
[Default: (null)]
指定安装软件包时临时启用的 yum 源

disablerepo : `Repoid’ of repositories to disable for the install/update operation. These repos will not persistbeyond the transaction. When specifying multiple repos, separate them with a “,”.
指定安装软件包时临时禁用的 yum 源

示例

案例1:安装最新版本的apache

- name: install the latest version of Apache
  dnf:
    name: httpd
    state: latest
[root@control ~]# ansible node1 -m dnf -a 'name=httpd  state=latest'

案例2:安装最新版本的apache和mariadb

- name: install the latest version of Apache and MariaDB
  dnf:
    name:
      - httpd
      - mariadb-server
state: latest
[root@control ~]# ansible node1 -m dnf -a 'name=httpd,mariadb-server state=latest'

案例3:卸载apache

- name: remove the Apache package
  dnf:
    name: httpd
    state: absent
[root@control ~]# ansible node1 -m dnf -a 'name=httpd state=absent'

案例4:使用testing源安装apache

- name: install the latest version of Apache from the testing repo
  dnf:
    name: httpd
    enablerepo: testing
    state: present
[root@control ~]# ansible node1 -m dnf -a 'name=httpd state=latest enablerepo=testing'

案例5:更新所有软件

- name: upgrade all packages
  dnf:
    name: "*"
state: latest
[root@control ~]# ansible node1 -m dnf -a 'name="*" state=latest'

案例6:根据远端rpm包安装软件

- name: install the nginx rpm from a remote repo
  dnf:
    name: 'http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm'
    state: present
[root@control ~]# ansible node1 -m dnf -a 'name="http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm" state=present'

案例7:根据本地rpm包安装软件

- name: install nginx rpm from a local file
  dnf:
    name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
    state: present
[root@control ~]# ansible node1 -m dnf -a 'name=" /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm" state=present'

案例8:安装Development tool软件集

- name: install the 'Development tools' package group
  dnf:
    name: '@Development tools'
    state: present
[root@control ~]# ansible node1 -m dnf -a 'name=" @Development tools" state=latest'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值