软件包管理
常用参数:
name参数:必须参数,用于指定需要管理的软件包,比如 nginx。
state参数:用于指定软件包的状态 ,默认值为。present,表示确保软件包已经安装,除了 present,其他可用值有 installed、latest、absent、removed,其中 installed 与present 等效,latest 表示安装 yum 中最新的版本,absent 和 removed 等效,表示删除对应的软件包。
检测没有 tree 安装包
[root@Study04 ansible]# ansible 127.0.0.1 -m shell -a “rpm -qa tree”
[WARNING]: Consider using the yum, dnf or zypper module rather than running rpm…
192.168.0.189 | CHANGED | rc=0 >>
执行命令批量安装:
[root@Study04 ansible]# ansible 127.0.0.1 -m yum -a “state=installed name=tree”
127.0.0.1 | CHANGED => {
“ansible_facts”: {
“pkg_mgr”: “yum”
},
“changed”: true,
“msg”: “”,
“rc”: 0,
“results”: [
“Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * elrepo: mirrors.tuna.tsinghua.edu.cn\n * epel: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n–> Running transaction check\n—> Package tree.x86_64 0:1.6.0-10.el7 will be installed\n–> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n tree x86_64 1.6.0-10.el7 base 46 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 46 k\nInstalled size: 87 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : tree-1.6.0-10.el7.x86_64 1/1 \n Verifying : tree-1.6.0-10.el7.x86_64 1/1 \n\nInstalled:\n tree.x86_64 0:1.6.0-10.el7 \n\nComplete!\n”
]
}
执行命令删除:
[root@Study04 ansible]# ansible 127.0.0.1 -m yum -a “state=removed name=tree”
127.0.0.1 | CHANGED => {
“ansible_facts”: {
“pkg_mgr”: “yum”
},
“changed”: true,
“msg”: “”,
“rc”: 0,
“results”: [
“已加载插件:fastestmirror, langpacks\n正在解决依赖关系\n–> 正在检查事务\n—> 软件包 tree.x86_64.0.1.6.0-10.el7 将被 删除\n–> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package 架构 版本 源 大小\n================================================================================\n正在删除:\n tree x86_64 1.6.0-10.el7 @base 87 k\n\n事务概要\n================================================================================\n移除 1 软件包\n\n安装大小:87 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在删除 : tree-1.6.0-10.el7.x86_64 1/1 \n 验证中 : tree-1.6.0-10.el7.x86_64 1/1 \n\n删除:\n tree.x86_64 0:1.6.0-10.el7 \n\n完毕!\n”
]
}