yum模块
yum模块可以帮助我们批量管理Linux远程主机软件包。
参数:
name:用于指定需要管理的软件包,比如 nginx。
state:状态 present,installed,removed,latest,absent。默认为present。
installed and present等效
latest标志安装yum中最新版本
absent and removed 等效 表示删除安装包
disable_gpg_check:用于禁用rmp包的公钥gpg验证,默认值no ,表示不做验证,设置为 yes 表示禁用验证,即不验证包,直接安装。在对应的 yum 源没有开启 gpg 验证的情况下,需要将此参数的值设置为 yes,否则会报错而无法进行安装.
enablerepo:用于指定安装软件包是临时启用的yum源。假如你想要从A源中安装软件,但是你不确定A源是否启用了,你可以在安装软件包时将此参数的值设置为 yes,即使A源的设置是未启用,也可以在安装软件包时临时启用A源。
Disablerepo:用于指定安装软件包是临时禁止用的yum源。当多个 yum源中同时存在要安装的软件包时,你可以使用此参数临时禁用某个源
Ansible命令行执行yum模块:
安装nginx软件包:
ansible all -m yum -a 'name=nginx disable_gpg_check=yes'
nginx软件包会被安装。
查看Nginx已经安装:
安装nginx软件包,临时关闭gpg校验:
ansible all -m yum -a 'name=nginx state=present disable_gpg_check=yes'
ansible all -m yum -a 'name=nginx state=installed disable_gpg_check=yes'
上面三种方法几乎一样。
Ansible批量卸载软件包:
ansible all -m yum -a 'name=nginx state=absent'
另一种写法:
ansible all -m yum -a 'name=nginx state=removed'
ansible只确认结果状态,没有改变就是绿色,有变化就是黄色,虽然没有安装,卸载一样返回成功。
指定临时源安装软件包:
ansible all -m yum -a 'name=telnet disable_gpg_check=yes enablerepo=base'
enablerepo=base:base源是centos自带的源,/etc/yum.repos.d/CentOS-Base.repo可以查看。
报错:
1.指定的临时源找不到 ,"msg": "Repository local not found."
[root@master ~]# ansible all -m yum -a 'name=telnet disable_gpg_check=yes enablerepo=local'10.40.42.127 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "Repository local not found."
}
10.40.42.103 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "Repository local not found."
}
指定的是源,不是yum源文件名:
# ansible all -m yum -a 'name=telnet disable_gpg_check=yes enablerepo=CentOS-Base'
Ansible批量安装多个软件包:
ansible all -m yum -a "name=nginx,telnet state=installed"
playbook中使用yum模块:
安装nginx软件包,默认安装最新的:
- hosts: alltasks:
- name: Install yum package in Ansible example
yum:
name: nginx
state: present
安装最新版的git软件包:
- hosts: alltasks:
- name: Install the latest yum package example.
yum:
name: git
state: latest
安装软件包组:
[root@master ~]# cat grouptool.yml- hosts: 10.40.42.127
tasks:
- name: install the 'Development tools' package group
yum:
name: "@Development tools"
state: present
安装两组软件包组:
[root@master ~]# cat grouptool.yml- hosts: 10.40.42.127
tasks:
- name: Install libvirt
yum:
name:
- "@Virtualization Hypervisor"
- "@Virtualization Client"
另一种写法:
[root@master ~]# cat grouptool.yml- hosts: 10.40.42.127
tasks:
- name: install the 'Development tools' package group
yum:
name: ["@Development tools","@Security Tools"]
指定url下载并安装软件包:
- name: install the nginx rpm from a remote repoyum:
name: http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
state: present
使用base源,安装nginx服务:
[root@master ~]# cat package.yml- hosts: all
tasks:
- name: Install yum package in Ansible example
yum:
name: nginx
state: installed
enablerepo: base
查看epel源和官方源nginx的版本相同,所以再次安装并没有什么改变:
安装指定版本的软件包:
- hosts: alltasks:
- name: Install a specific version of a package in Ansible.
yum:
name: git-1.8.3.1-23.el7_8.x86_64
state: present
批量安装多个软件包:
[root@master ~]# cat allpackages.yml- hosts: all
tasks:
- name: yum
yum:
name: "{{ item }}"
state: present
with_items:
- nginx
- httpd
- telnet
第二种写法:批量安装软件包:
[root@master ~]# cat allpackages.yml- hosts: all
tasks:
- name: yum
yum:
name: "{{ packages }}"
vars:
packages:
- nginx
- telnet
- httpd
第三种种写法:
[root@master ~]# cat allpackages.yml- hosts: all
tasks:
- name: yum
yum:
name:
- nginx
- telnet
- httpd
state: present
第四种写法:
[root@master ~]# cat allpackages.yml- hosts: all
tasks:
- name: yum
yum:
name: "{{ item }}"
state: present
loop:
- nginx
- telnet
- httpd
第五种写法:
[root@master ~]# cat allpackages.yml- hosts: all
tasks:
- name: yum
yum:
name: ["nginx","httpd","telnet"]
state: present
批量卸载多个软件包:
[root@master ~]# cat allpackages.yml- hosts: all
tasks:
- name: yum
yum:
name: "{{ packages }}"
state: removed
vars:
packages:
- nginx
- telnet
- httpd
只下载软件包,但是不安装:
[root@master ~]# cat package.yml- hosts: all
tasks:
- name: Install yum package in Ansible example
yum:
name:
- telnet
state: latest
download_only: true
更新所有软件包:
你可以更新所有的yum软件包,例如使用命令yum -y update。你可以在名称中使用通配符“ *”。
- hosts: alltasks:
- name: Upgrade all yum packages ansible.
yum:
name: "*"
state: latest
使用exclude参数,以便某些软件包不应该升级。以下任务不会更新git软件包:
- hosts: alltasks:
- name: Exclude some packages from an upgrade in Ansible.
yum:
name: "*"
state: latest
exclude: git*
Yum更新升级软件包,排除kernel*,foo*的软件包:
- name: upgrade all packages, excluding kernel & foo related packagesyum:
name: '*'
state: latest
exclude: kernel*,foo*