19-Ansible常用模块-yum_repository模块,


一、概述

yum_repository 模块可以帮助我们管理远程主机上的 yum 仓库。

二、常用参数

name参数:必须参数,用于指定要操作的唯一的仓库ID,也就是”.repo”配置文件中每个仓库对应的”中括号”内的仓库ID。

baseurl参数:此参数用于设置 yum 仓库的 baseurl。

description参数:此参数用于设置仓库的注释信息,也就是”.repo”配置文件中每个仓库对应的”name字段”对应的内容。

file参数:此参数用于设置仓库的配置文件名称,即设置”.repo”配置文件的文件名前缀,在不使用此参数的情况下,默认以 name 参数的仓库ID作为”.repo”配置文件的文件名前缀,同一个”.repo” 配置文件中可以存在多个 yum 源。

enabled参数:此参数用于设置是否激活对应的 yum 源,此参数默认值为 yes,表示启用对应的 yum 源,设置为 no 表示不启用对应的 yum 源。

gpgcheck参数:此参数用于设置是否开启 rpm 包验证功能,默认值为 no,表示不启用包验证,设置为 yes 表示开启包验证功能。

gpgcakey参数:当 gpgcheck 参数设置为 yes 时,需要使用此参数指定验证包所需的公钥。

state参数:默认值为 present,当值设置为 absent 时,表示删除对应的 yum 源。

三、示例

1.在 ansible-demo3 主机上设置ID为 aliEpel 的 yum 源,仓库配置文件路径为 /etc/yum.repos.d/aliEpel.repo

[root@ansible-manager ~]# ansible ansible-demo3 -m yum_repository -a 'name=aliEpel description="alibaba EPEL" baseurl=https://mirrors.aliyun.com/epel/$releasever\Server/$basearch/'
ansible-demo3 | SUCCESS => {
    "changed": true, 
    "repo": "aliEpel", 
    "state": "present"
}

2.在 ansible-demo3 主机上设置ID为 aliEpel 的 yum 源,仓库配置文件路径为 /etc/yum.repos.d/alibaba.repo

[root@ansible-manager ~]# ansible ansible-demo3 -m yum_repository -a 'name=aliEpel description="alibaba EPEL" baseurl=https://mirrors.aliyun.com/epel/$releasever\Server/$basearch/ file=alibaba'
ansible-demo3 | SUCCESS => {
    "changed": true, 
    "repo": "aliEpel", 
    "state": "present"
}

3.在 ansible-demo3 主机上设置ID为 local 的 yum 源,但是不启用它(local源使用系统光盘镜像作为本地 yum 源,以便测试举例,所以 baseurl 中的值以 file:/// 开头)。

ansible ansible-demo3 -m yum_repository -a 'name=local baseurl=file:///media description="local cd yum" enabled=no'

4.在 ansible-demo3 主机上设置ID为 local 的 yum 源,开启包验证功能,并指定验证包所需的公钥位置为 /media/RPM-GPG-KEY-CentOS-7

ansible ansible-demo3  -m yum_repository -a 'name=local baseurl=file:///media description="local cd yum" gpgcheck=yes gpgcakey=file:///media/RPM-GPG-KEY-CentOS-7'

5.在 ansible-demo3 主机上删除 /etc/yum.repos.d/alibaba.repo 配置文件中的 aliEpel 源。

[root@ansible-manager ~]# ansible ansible-demo3 -m yum_repository -a 'file=alibaba name=aliEpel state=absent'
ansible-demo3 | SUCCESS => {
    "changed": true, 
    "repo": "aliEpel", 
    "state": "absent"
}

四、总结

本节介绍了 Ansible 常用模块之 yum_repository 模块,并举例说明如何使用,下节我们介绍 yum 模块。

这里的命令常用参数介绍及示例,主要摘抄自下面文档内容,有部分改动,在这里对原作者深表感谢!

参考文档:
http://www.zsythink.net/archives/2592

相关内容

    暂无相关文章