侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 748 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

ansible的service模块

zze
zze
2020-03-28 / 0 评论 / 0 点赞 / 773 阅读 / 1774 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

此文章为「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,此参数的可用值有 startedstoppedrestartedreloaded
  • 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"
0

评论区