ansible(9)--ansible的yum模块
1. yum模块
功能:管理软件包,需要确认被管理端为红帽系列的,并且需要被管理端配置好yum源。
主要的参数如下:
参数 | 说明 |
---|---|
name | 指定安装软件包名或软件包URL |
state | 指定yum对应的方法,present(Defaults)表示安装;absent表示卸载;latest表示安装最新版本软件包,支持多程序一起安装,用逗号隔开 |
enablerepo | 允许从哪些仓库获取软件 |
disablerepo | 禁止从哪些仓库获取软件 |
exclude | 排除某些软件包,例如kernel |
download_only | 仅下载软件包,不安装 |
disable_gpg_check | 不进行gpg检测 |
update_cache | 可以在安装包的同时更新yum缓存 |
-
示例一:在被控端安装
vsftpd
,apache
软件包:[root@xuzhichao ~]# ansible 192.168.20.23 -m yum -a 'name=vsftpd,httpd state=present' 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "installed": [ "vsftpd", "httpd" ] }, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-93.el7.centos will be installed\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-93.el7.centos.x86_64\n---> Package vsftpd.x86_64 0:3.0.2-27.el7 will be installed\n--> Running transaction check\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-93.el7.centos base 2.7 M\n vsftpd x86_64 3.0.2-27.el7 base 172 k\nInstalling for dependencies:\n mailcap noarch 2.1.41-2.el7 base 31 k\n\nTransaction Summary\n================================================================================\nInstall 2 Packages (+1 Dependent package)\n\nTotal download size: 2.9 M\nInstalled size: 9.8 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal 119 MB/s | 2.9 MB 00:00 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : mailcap-2.1.41-2.el7.noarch 1/3 \n Installing : httpd-2.4.6-93.el7.centos.x86_64 2/3 \n Installing : vsftpd-3.0.2-27.el7.x86_64 3/3 \n Verifying : mailcap-2.1.41-2.el7.noarch 1/3 \n Verifying : vsftpd-3.0.2-27.el7.x86_64 2/3 \n Verifying : httpd-2.4.6-93.el7.centos.x86_64 3/3 \n\nInstalled:\n httpd.x86_64 0:2.4.6-93.el7.centos vsftpd.x86_64 0:3.0.2-27.el7 \n\nDependency Installed:\n mailcap.noarch 0:2.1.41-2.el7 \n\nComplete!\n" ] } [root@nginx03 ~]# rpm -q httpd httpd-2.4.6-93.el7.centos.x86_64 [root@nginx03 ~]# rpm -q vsftpd vsftpd-3.0.2-27.el7.x86_64
-
示例二:安装当前最新的
Apache
软件,通过epel
仓库安装:[root@xuzhichao ~]# ansible webservers -m yum -a "name=httpd state=present enablerepo=epel"
-
示例三:通过公网
URL
安装rpm
软件:[root@xuzhichao ~]# ansible webservers -m yum -a "name=https://xx.rpm state=present"
-
示例四:卸载被控主机的
vsftpd
,apache
软件包:[root@xuzhichao ~]# ansible 192.168.20.23 -m yum -a 'name=vsftpd,httpd state=absent'
-
示例五:安装最新版本的
Apache
软件,如果存在则更新Apache
:[root@xuzhichao ~]# ansible webservers -m yum -a "name=httpd state=latest
-
示例六:更新所有的软件包,但排除和
kernel
相关的:[root@xuzhichao ~]# ansible webservers -m yum -a "name=* state=latest exclude=kernel"