此文章为「ansible笔记(2)之常用模块」的子文章。
service 模块可以帮助我们管理远程主机上的服务,比如,启动或停止远程主机中的 nginx 服务。
注意:假如你想要管理远程主机中的某个服务,那么这个服务必须能被 BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart 中的任意一种所管理,否则 service 模块也无法管理远程主机的对应服务,这样说可能不容易理解,那么我们换个方式来解释,假设你在使用 CentOS 6,那么你的 CentOS 6 中的 nginx 则必须能够通过
service nginx start启动,如果你的 nginx 无法通过service nginx start进行启动,那么它将同样无法通过 ansible 的 service 模块启动,假设你在使用CentOS 7,那么你的 CentOS 7 中的 nginx 则必须能够通过systemctl start nginx启动,如果它无法通过systemctl start nginx进行启动,那么它将同样无法通过 ansible 的 service 模块进行启动,CentOS 6 中默认通过 sysv 管理服务,CentOS 7 中默认通过 systemd 管理服务,如果你的服务无法通过 BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart 中的任意一种所管理,那么它也无法被 ansible 的 service 模块管理。
参数说明:
name:此参数用于指定需要操作的服务名称,比如nginx;state:此参数用于指定服务的状态,比如,我们想要启动远程主机中的 nginx,则可以将state的值设置为started,如果想要停止远程主机中的服务,则可以将state的值设置为stopped,此参数的可用值有started、stopped、restarted、reloaded;enabled:此参数用于指定是否将服务设置为开机 启动项,设置为yes表示将对应服务设置为开机启动,设置为no表示不会开机启动;
例 1:让客户机中的 nginx 服务处于启动状态。
$ ansible all -m service -a "name=nginx state=started"
例 2:将客户机中的 nginx 服务处于停止状态。
$ ansible all -m service -a "name=nginx state=stopped"
例 3:将客户机中的 nginx 服务被设置为开机自动启动项。
$ ansible all -m service -a " name='nginx' enabled=yes"
评论区