handlers01.yaml
---
- hosts: ubuntu
handlers:
- name: restart apache
service: name=apache2 state=restarted
tasks:
- name: 确保APT包列表是最新的
apt: update_cache=yes
- name: 部署apache2
apt: name=apache2 state=present
- name: 设置开机自启
service: name=apache2 enabled=yes
- name: 开启Apache rewrite模块
apache2_module: name=rewrite state=present
notify: restart apache
root@test:~/handlers# ansible-playbook handlers01.yaml
PLAY [ubuntu] ******************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************
ok: [192.168.255.110]
TASK [确保APT包列表是最新的] ************************************************************************************************************************
changed: [192.168.255.110]
TASK [部署apache2] ***************************************************************************************************************************
ok: [192.168.255.110]
TASK [设置开机自启] ******************************************************************************************************************************
ok: [192.168.255.110]
TASK [开启Apache rewrite模块] ******************************************************************************************************************
ok: [192.168.255.110]
PLAY RECAP *********************************************************************************************************************************
192.168.255.110 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
handlers02.yaml
---
- hosts: ubuntu
handlers:
- name: restart apache
service: name=apache2 state=restarted
notify: restart memcached
- name: restart memcached
service: name=memcached state=restarted
tasks:
- name: 确保APT包列表是最新的
apt: update_cache=yes
- name: 部署apache2
apt: name=apache2 state=present
- name: 设置开机自启
service: name=apache2 enabled=yes
- name: 开启Apache rewrite模块
apache2_module: name=rewrite state=present
notify: restart apache
- name: 部署memcached
apt: name=memcached state=present
- name: 设置开机自启
service: name=memcached enabled=yes state=started
- name: 创建目录
file:
path: /etc/memcached/
state: directory
mode: 0755
- name: 指定配置文件
template:
src: templates/memcached.conf.j2
dest: /etc/memcached/memcached.conf
mode: 0755
#这里通过changed_when设置为修改了配置文件,从而触发重启apache,同时重启memcached
changed_when: true
notify:
- restart apache
- name: 部署vim
apt: name=vim state=present
root@test:~/handlers# ansible-playbook handlers02.yaml
PLAY [ubuntu] ******************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************
ok: [192.168.255.110]
TASK [确保APT包列表是最新的] ************************************************************************************************************************
changed: [192.168.255.110]
TASK [部署apache2] ***************************************************************************************************************************
ok: [192.168.255.110]
TASK [设置开机自启] ******************************************************************************************************************************
ok: [192.168.255.110]
TASK [开启Apache rewrite模块] ******************************************************************************************************************
ok: [192.168.255.110]
TASK [部署memcached] *************************************************************************************************************************
ok: [192.168.255.110]
TASK [设置开机自启] ******************************************************************************************************************************
ok: [192.168.255.110]
TASK [创建目录] ********************************************************************************************************************************
ok: [192.168.255.110]
TASK [指定配置文件] ******************************************************************************************************************************
changed: [192.168.255.110]
TASK [部署vim] *******************************************************************************************************************************
ok: [192.168.255.110]
RUNNING HANDLER [restart apache] ***********************************************************************************************************
changed: [192.168.255.110]
RUNNING HANDLER [restart memcached] ********************************************************************************************************
changed: [192.168.255.110]
PLAY RECAP *********************************************************************************************************************************
192.168.255.110 : ok=12 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
---
- hosts: ubuntu
handlers:
- name: restart apache
service: name=apache2 state=restarted
notify: restart memcached
- name: restart memcached
service: name=memcached state=restarted
tasks:
- name: 确保APT包列表是最新的
apt: update_cache=yes
- name: 部署apache2
apt: name=apache2 state=present
- name: 设置开机自启
service: name=apache2 enabled=yes
- name: 开启Apache rewrite模块
apache2_module: name=rewrite state=present
notify: restart apache
- name: 部署memcached
apt: name=memcached state=present
- name: 设置开机自启
service: name=memcached enabled=yes state=started
- name: 创建目录
file:
path: /etc/memcached/
state: directory
mode: 0755
- name: 指定配置文件
template:
src: templates/memcached.conf.j2
dest: /etc/memcached/memcached.conf
mode: 0755
#这里通过changed_when设置为修改了配置文件,从而触发重启apache,同时重启memcached
changed_when: true
notify:
- restart apache
- name: 立即执行handlers
meta: flush_handlers
- name: 部署vim
apt: name=vim state=present
root@test:~/handlers# ansible-playbook handlers03.yaml
PLAY [ubuntu] ******************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************
ok: [192.168.255.110]
TASK [确保APT包列表是最新的] ************************************************************************************************************************
changed: [192.168.255.110]
TASK [部署apache2] ***************************************************************************************************************************
ok: [192.168.255.110]
TASK [设置开机自启] ******************************************************************************************************************************
ok: [192.168.255.110]
TASK [开启Apache rewrite模块] ******************************************************************************************************************
ok: [192.168.255.110]
TASK [部署memcached] *************************************************************************************************************************
ok: [192.168.255.110]
TASK [设置开机自启] ******************************************************************************************************************************
ok: [192.168.255.110]
TASK [创建目录] ********************************************************************************************************************************
ok: [192.168.255.110]
TASK [指定配置文件] ******************************************************************************************************************************
changed: [192.168.255.110]
RUNNING HANDLER [restart apache] ***********************************************************************************************************
changed: [192.168.255.110]
RUNNING HANDLER [restart memcached] ********************************************************************************************************
changed: [192.168.255.110]
TASK [部署vim] *******************************************************************************************************************************
ok: [192.168.255.110]
PLAY RECAP *********************************************************************************************************************************
192.168.255.110 : ok=12 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Handlers只有在其所在的任务被执行时,才会被运行
如果一个任务中定义了notify调用Handlers,但是由于条件判断等原因,该任务未被执行,那么Handlers同样不会被执行
Handlers只会在Play的末尾运行一次;
如果想在一个Playbook的中间运行Handlers,则需要使用meta模块来实现,
meta: flush_handlers
标签:TASK,ok,name,handlers,ansible,192.168,255.110,memcached From: https://www.cnblogs.com/anyux/p/18356540