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