Ansible Service模块手册

但使书种多,会有岁稔时。这篇文章主要讲述Ansible Service模块手册相关的知识,希望能为你提供帮助。
Service模块启动nginx服务:
ansible all -m service -a "name=nginx state=started"

Ansible Service模块手册

文章图片

关闭nginx服务:
ansible all -m service -a "name=nginx state=stopped"
Ansible Service模块手册

文章图片

设置nginx开机自启动:
ansible all -m service -a "name=\'nginx\' enabled=yes"
Ansible Service模块手册

文章图片

 
重启nginx服务,设置开机自启动:
ansible all -m service -a \'name=nginx state=restarted enabled=yes\'
Ansible Service模块手册

文章图片

Playbook service模块写法:启动nginx服务,并设置开机自启动。

[root@master ~]# cat svc.yml
- hosts: all
tasks:
- name: Enable service httpd, and not touch the state
service:
name: nginx
state: started
enabled: yes

 
Ansible Service模块手册

文章图片

下面这个参数不会使用,本来想尝试reids和mongo的,结果发现不行,哪位大佬知道咋用》评论区回复。
- name: Start service foo, based on running process /usr/bin/foo
    service:
        name: foo
        pattern: /usr/bin/foo
        state: started
 
Service模块中 notify、 handlers触发器的使用:

[root@master ~]# cat svc2.yml
---
- hosts: all
remote_user: root
vars:
- pkg: nginx
tasks:
- name: "install nginx package."
yum: name={{ pkg }}state=installed
- name: "copy httpd configure file to remote host."
copy: content="hello mew" dest=/usr/share/nginx/index.html
notify: restart nginx# 当这个任务执行状态发生改变时,触发handlers执行.
- name: "boot httpd service."
service: name=nginx state=started
handlers:# handlers与tasks是同一级别
- name: restart nginx
service: name=nginx state=restarted

 
Ansible Service模块手册

文章图片

【Ansible Service模块手册】 

    推荐阅读