yum模块用于在python2环境下管理基于RPM的Linux发行版中的rpm包,在python3环境中使用dnf模块。
yum模块常用参数
name:必须参数,指定要操作的包名,同时可以指定版本,,如果指定了以前的版本,需要打开allow_downgrade参数;如果state参数为latest,name参数可以指定为'*',这意味着yum -y update;如果指定了本地的rpm文件或是一个url连接,需要state参数为present。
allow_downgrade:是否允许rpm包版本降级(True或False)
state:安装 (present or installed, latest) 或删除 (absent or removed) 包,
download_only:仅下载,不安装
download_dir:与download_only参数一起使用,指定下载包的目录
disable_gpg_check:当state参数值为present或latest时,禁用gpg检查
list:列出包的安装,更新,可用以及仓库信息,相当于yum list
yum模块示例
1、安装php和mariadb
- name: install php and mariadb
yum: name= "{{ item }}"
with_items:
- php
- mariadb
2、安装Development Tools包组
- name: install Development Tools
hosts: dev
tasks:
- name: install development tools
yum: name="@Development Tools"
3、升级主机上的软件包到最新版本
- name: update for all
hosts: dev
tasks:
- name: update
yum: name="*" state=latest
4、移除httpd
- name: remove httpd
yum: name= httpd state=absent
5、升级主机上的软件包到最新版本,除去内核
- name: Upgrade removing the kernel
hosts: dev
tasks:
- name: update
yum: name="*" state=latest exclude=kernel*
6、从url安装包
- 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
7、从本地rpm包安装
- name: install nginx rpm from a local file
yum:
name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
state: present
8、列出ansible相关的包
- name: List ansible packages and register result to print with debug later.
yum:
list: ansible
register: result
9、只下载,不安装
- name: Download the nginx package but do not install it
yum:
name:
- nginx
state: latest
download_only: true
download_dir: /root/nginx_rpms/
参考:ansible-doc yum
来源:oschina
链接:https://my.oschina.net/u/4281394/blog/4907142