ansible使用包管理模块
一般使用的包管理模块的RPM,和YUM
参数
|
必填
|
默认
|
选择
|
说明
|
Conf_file
|
No
|
|
|
YUM的配置文件
|
Disable_dbg_check
|
No
|
No
|
Yes/no
|
是否开启验证
|
Disablerepo
|
No
|
|
|
禁用的仓库
|
Enablerepo
|
No
|
|
|
启用的仓库
|
List
|
No
|
|
|
非幂等性命令
|
Name
|
Yes
|
|
|
包名称
|
State
|
No
|
Present
|
Present/installed
Latest/absent
Removed
|
是否插入(present,installed,latest),移除
(absent/removed)包
|
1.1 安装最新的包
[root@ansibleserver tmp]# ansible pythonserver -m yum -a "name=httpd state=latest"
SSH password:
192.168.1.60 | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: product-id, security, subscription-manager\nUpdating certificate-based repositories.\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-15.el6 will be installed\n-->
Processing Dependency: httpd-tools = 2.2.15-15.el6 for package: httpd-2.2.15-15.el6.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-15.el6.x86_64\n--> Running transaction check\n---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1
will be installed\n---> Package httpd-tools.x86_64 0:2.2.15-15.el6 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.2.15-15.el6 rhel-source-beta 808 k\nInstalling for dependencies:\n
apr-util-ldap x86_64 1.3.9-3.el6_0.1 rhel-source-beta 15 k\n httpd-tools x86_64 2.2.15-15.el6 rhel-source-beta 70 k\n\nTransaction Summary\n================================================================================\nInstall
3 Package(s)\n\nTotal download size: 893 k\nInstalled size: 3.1 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal 25 MB/s | 893 kB 00:00 \nRunning
rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64 1/3 \n\r Installing : httpd-tools-2.2.15-15.el6.x86_64 2/3
\n\r Installing : httpd-2.2.15-15.el6.x86_64 3/3 \nInstalled products updated.\n\nInstalled:\n httpd.x86_64 0:2.2.15-15.el6 \n\nDependency Installed:\n apr-util-ldap.x86_64
0:1.3.9-3.el6_0.1 httpd-tools.x86_64 0:2.2.15-15.el6 \n\nComplete!\n"
]
}
|
查询之后发现已经安装,并且安装了最新版本
name表示需要安装的包名称,state为lastest,表示安装Apache的最新版本
1.2 移除包
[root@ansibleserver tmp]# ansible pythonserver -m yum -a "name=httpd state=absent"
SSH password:
192.168.1.60 | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: product-id, security, subscription-manager\nUpdating certificate-based repositories.\nSetting up Remove Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-15.el6 will be erased\n-->
Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n
httpd x86_64 2.2.15-15.el6 @rhel-source-beta 2.9 M\n\nTransaction Summary\n================================================================================\nRemove 1 Package(s)\n\nInstalled size: 2.9 M\nDownloading Packages:\nRunning
rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Erasing : httpd-2.2.15-15.el6.x86_64 1/1 \nInstalled products updated.\n\nRemoved:\n httpd.x86_64 0:2.2.15-15.el6
\n\nComplete!\n"
]
}
|
移除安装包
1.3 指定repo来进行安装包
[root@ansibleserver tmp]# ansible pythonserver -m yum -a "name=httpd enablerepo=rhel state=present"
SSH password:
192.168.1.60 | FAILED >> {
"failed": true,
"msg": "Error setting/accessing repo rhel: Error getting repository data for rhel, repository not found"
}
|
在指定源的时候,使用enablerepo,注意这个名字是配置文件/etc/yum.repo.d/目录中文件的section的名称,如下所示:
[root@ansibleserver yum.repos.d]# cat rhel-source.repo |grep kel
[kel]
name=kel
baseurl=file:///tmp/kel
|
主要的就是使用这个repo的名字,section为[kel]
在上面的报错信息中,主要就是没有找到这个section
[root@ansibleserver ~]# ansible pythonserver -m yum -a "name=httpd enablerepo=kel state=present"
SSH password:
192.168.1.60 | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: product-id, security, subscription-manager\nUpdating certificate-based repositories.\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-15.el6 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.2.15-15.el6 kel 808 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package(s)\n\nTotal download size: 808 k\nInstalled size:
2.9 M\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : httpd-2.2.15-15.el6.x86_64 1/1 \nInstalled products updated.\n\nInstalled:\n
httpd.x86_64 0:2.2.15-15.el6 \n\nComplete!\n"
]
}
|
在上面例子中,可以看到直接进行了安装
1.4 指定版本进行安装包
[root@ansibleserver ~]# ansible pythonserver -m yum -a "name=httpd-2.2.15-15.el6.x86_64 state=present "
SSH password:
192.168.1.60 | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: product-id, security, subscription-manager\nUpdating certificate-based repositories.\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-15.el6 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.2.15-15.el6 kel 808 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package(s)\n\nTotal download size: 808 k\nInstalled size:
2.9 M\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : httpd-2.2.15-15.el6.x86_64 1/1 \nInstalled products updated.\n\nInstalled:\n
httpd.x86_64 0:2.2.15-15.el6 \n\nComplete!\n"
]
}
|
在进行name指定版本的时候,架构类型可以写也可以不写,也就是x86_64可以写或者不写都是可以的
在写上版本的时候,注意是l还是1,注意具体的版本型号,在实验的时候因为字母l和数字1导致很多时间被浪费,指定的具体版本无法进行安装
1.5 更新所有安装包
[root@ansibleserver ~]# ansible pythonserver -m yum -a "name=* state=latest"
|
1.6 指定rpm进行安装
[root@ansibleserver ~]# ansible pythonserver -m yum -a "name=/usr/local/src/kel.noarch.rpm state=present"
|
表示直接指定了一个rpm的位置,然后进行直接安装rpm
1.7 指定远程网址rpm进行安装
ansible pythonserver -m yum -a "name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6.0.el6.ngx.noarch.rpm state=present"
|
直接指定一个远程网址来进行安装包