Service模块 启动nginx服务: ansible all -m service -a "name=nginx state=started" 关闭nginx服务: ansible all -m service -a "name=nginx state=stopped" 设置nginx开机自启动: ansible all -m service -a "name='nginx' enabled=yes" 重
Service模块
启动nginx服务:
ansible all -m service -a "name=nginx state=started"
关闭nginx服务:
ansible all -m service -a "name=nginx state=stopped"
设置nginx开机自启动:
ansible all -m service -a "name='nginx' enabled=yes"
重启nginx服务,设置开机自启动:
ansible all -m service -a 'name=nginx state=restarted enabled=yes'
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
下面这个参数不会使用,本来想尝试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