ansible yum 模块
概要
- 使用yum包管理器安装、升级、降级、删除和列出包和组
- yum模块仅适用于python2,若使用python3则用dnf模块
参数
parameter(参数) | choices/default | comments(注解) |
allow_downgrade | yes or no,默认no | 指定指定的包和版本是否允许降级该包可能已经安装的更高版本。注意,设置allow_降级=True会使此模块以非幂等方式运行。该任务最终可能会得到一组与要安装的指定包的完整列表不匹配的包(因为降级的包与其他包之间的依赖关系可能会导致对先前事务中的包的更改)。 |
autoremove | yes or no,默认no | 如果 注意:该特性需要yum >= 3.4.3 (RHEL/CentOS 7+) |
bugfix | yes or no,默认no | 如果设置为yes,并且state=latest,则只安装标记为与bug修复相关的更新。 |
conf_file | 用于事务的远程yum配置文件。 | |
disable_excludes | 禁用YUM配置文件中定义的排除。 如果设置为all,则禁用所有排除。 如果设置为main,禁用yum.conf中[main]中定义的排除。 如果设置为repoid,禁用为给定的repoid定义的排除。 | |
disable_gpg_check | yes or no,默认no | 是否对正在安装的包进行GPG签名检查。仅当state为present或latest时才生效。 |
disable_plugin | 安装/更新操作中禁用的插件名称。被禁用的插件将不会在事务之外持久存在。 | |
disablerepo | Repoid存储库以禁用安装/更新操作。这些回购不会在交易结束后继续存在。当指定多个回购时,用“,”隔开。 在Ansible 2.7中,这也可以是一个列表,而不是“,”分隔字符串 | |
download_dir | 指定存储包的备用目录。 只有在指定download_only时才会生效。 | |
download_only | yes or no,默认no | 只下载,不安装。 |
enable_plugin | 用于安装/更新操作的插件名称。启用的插件将不会在事务之后持久存在。 | |
enablerepo | Repoid存储库以启用安装/更新操作。这些回购不会在交易结束后继续存在。当指定多个回购时,用“,”隔开。 在Ansible 2.7中,这也可以是一个列表,而不是“,”分隔字符串 | |
exclude | 当state=present或latest时要排除的包名 | |
install_weak_deps | yes or no,默认yes | 也将安装由弱依赖关系链接的所有包。 注意:该特性需要yum >= 4 (RHEL/CentOS 8+) |
installroot | 默认"/" | 指定一个替代的installroot,所有的包都将相对于它安装。 |
list | 要运行等价于yum list——show-duplicate < Package >的包名。除了列出软件包,use还可以列出以下:已安装、更新、可用和回购。 与name互斥。 | |
lock_timeout | 默认30s | 等待yum锁定文件释放的时间。 |
name | 带有版本的包名或包说明符,如name-1.0。 如果指定了以前的版本,任务还需要打开allow_降级。有关降级包的注意事项,请参阅allow_降级文档。 当使用state=latest时,可以是’*',这意味着运行yum -y update。 您还可以传递一个url或一个本地路径到rpm文件(使用state=present)。要操作多个包,可以使用逗号分隔的包字符串或(从2.0开始)包列表。 | |
releasever | 指定将从其中安装所有包的替代版本。 | |
security | yes or no,默认no | 如果设置为yes,并且state=latest,则只安装标记为安全相关的更新。 |
skip_broken | yes pr no,默认no | 跳过依赖关系损坏的包(devsolve)并导致问题。 |
state | absent installed latest present removed | 是否安装(
默认为 |
update_cache | yes or no,默认no | 强制yum检查缓存是否过期,并在需要时重新下载。仅当state为present或latest时才生效。 |
update_only | yes or no,默认no | 使用latest时,只更新已安装的软件包。请勿安装软件包。 作为效果只有当state是latest |
use_backend | auto(默认) yum yum4 dnf | 这个模块支持yum(一如既往),被上游yum开发者称为yum3/ yum3/ yum-deprecated。在Ansible 2.7+版本中,这个模块也支持YUM4,这是一个“新yum”,它有一个dnf后端。 默认情况下,该模块将基于ansible_pkg_mgr事实选择后端。 |
validate_certs | yes or no,默认yes | 这只适用于使用https url作为rpm的源。例如localinstall。如果设置为no,将不验证SSL证书。 在使用自签名证书的个人控制网站上,这只应该设置为no,因为它避免了验证源网站。 在2.1版本之前,这段代码的工作方式就好像设置为yes一样。 |
笔记
- 当与循环一起使用时:将单独处理每个包,将列表直接传递给name选项效率更高。
- 在 1.9.2 之前的版本中,此模块分别安装和删除了给 yum 模块的每个包。当文件名或 url 指定的包必须一起安装或删除时,这会导致问题。在 1.9.2 中,此问题已得到修复,以便将软件包安装在一个 yum 事务中。但是,如果其中一个软件包添加了其他软件包来自的新 yum 存储库(例如 epel-release),则该软件包需要安装在单独的任务中。这模仿了 yum 的命令行行为。
- Yum 本身有两种类型的组。“包组”在 rpm 本身中指定,而“环境组”在单独的文件中指定(通常由发行版)。不幸的是,这种划分对 ansible 用户来说变得很明显,因为 ansible 需要在单个事务中对包组进行操作,而 yum 要求在以这种方式使用时以不同的方式指定组。包组指定为“@development-tools”,环境组指定为“@^gnome-desktop-environment”。使用“yum group list hidden ids”命令查看您要安装的组属于哪个组。
- yum模块不支持幂等清除yum缓存,所以决定不实现,唯一的方法是使用command,直接调用yum命令,即“command: yum clean all” https://github .com/ansible/ansible/pull/31450#issuecomment-352889579
例子
- name: install the latest version of Apache
yum:
name: httpd
state: latest
- name: ensure a list of packages installed
yum:
name: "{{ packages }}"
vars:
packages:
- httpd
- httpd-tools
- name: remove the Apache package
yum:
name: httpd
state: absent
- name: install the latest version of Apache from the testing repo
yum:
name: httpd
enablerepo: testing
state: present
- name: install one specific version of Apache
yum:
name: httpd-2.2.29-1.4.amzn1
state: present
- name: upgrade all packages
yum:
name: '*'
state: latest
- name: upgrade all packages, excluding kernel & foo related packages
yum:
name: '*'
state: latest
exclude: kernel*,foo*
- name: install the nginx rpm from a remote repo
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
yum:
name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
state: present
- name: install the 'Development tools' package group
yum:
name: "@Development tools"
state: present
- name: install the 'Gnome desktop' environment group
yum:
name: "@^gnome-desktop-environment"
state: present
- name: List ansible packages and register result to print with debug later.
yum:
list: ansible
register: result
- name: Install package with multiple repos enabled
yum:
name: sos
enablerepo: "epel,ol7_latest"
- name: Install package with multiple repos disabled
yum:
name: sos
disablerepo: "epel,ol7_latest"
- name: Install a list of packages
yum:
name:
- nginx
- postgresql
- postgresql-server
state: present
- name: Download the nginx package but do not install it
yum:
name:
- nginx
state: latest
download_only: true