ansible playbook基本格式和handler

参考:http://www.zsythink.net/archives/2602

playbook基本格式:

playbook文件以“.yml”或“.yaml”为后缀

[root@localhost ~]#vim test.yml
---
- hosts: A
  remote_user: root
  tasks:
  - name: install nginx
    yum: name=nginx state=installed
  - name: hostname
    shell: echo $HOSTNAME

或者:
---
- hosts: A
  remote_user: root
  tasks:
  - name: install nginx
    yum: 
      name: nginx 
      state: installed
  - name: hostname
    shell: echo $HOSTNAME
    
语法检查:
[root@localhost ~]#ansible-playbook --syntax-check /root/test.yml 

playbook: /root/test.yml				#当只返回剧本名字时表示格式没问题

干跑测试:
[root@localhost ~]#ansible-playbook -C|--check /root/test.yml 

handers的用法:

假设要把nginx的默认监听端口改为8080,并且在修改配置文件后重启服务
[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  tasks:
  - name: configure nginx port
    replace: path=/etc/nginx/nginx.conf regexp=\<80\> replace=8080
  - name: restart nginx
    service: name=nginx state=restarted
    
[root@localhost ~]#ansible-playbook --syntax-check test.yml 

playbook: test.yml

[root@localhost ~]#ansible-playbook test.yml 

PLAY [B] **********************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.91.132]

TASK [configure nginx port] ***************************************************************************************
changed: [192.168.91.132]

TASK [restart nginx] **********************************************************************************************
changed: [192.168.91.132]

PLAY RECAP ********************************************************************************************************
192.168.91.132             : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

查看端口
[root@centos7 ~]#ss -ntl
State       Recv-Q Send-Q            Local Address:Port                           Peer Address:Port              
LISTEN      0      128                           *:8080                                      *:*                  
端口修改成功并且服务已启动

因为ansible的幂等性,当目标状态与预期的状态一致时,ansible不会做任何改动,因此当再次执行剧本时,被控主机的配置文件不会做任何修改,但是服务会重新启动。这是不应该的

[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  tasks:
  - name: configure nginx port
    replace: path=/etc/nginx/nginx.conf regexp=\<80\> replace=8080
    notify:
     restart nginx

  handlers:				#与tasks对齐
  - name: restart nginx				#name的值要与notify一样
    service: name=nginx state=restarted
    
[root@localhost ~]#ansible-playbook test.yml 

PLAY [B] **********************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.91.132]

TASK [configure nginx port] ***************************************************************************************
ok: [192.168.91.132]

PLAY RECAP ********************************************************************************************************
192.168.91.132             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
因为“[configure nginx port]”段没有改变,所以handlers没有执行。

修改剧本:
[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  tasks:
  - name: configure nginx port
    replace: path=/etc/nginx/nginx.conf regexp=8080 replace=80
    notify:
     restart nginx

  handlers:
  - name: restart nginx
    service: name=nginx state=restarted

[root@localhost ~]#ansible-playbook test.yml 

PLAY [B] **********************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.91.132]

TASK [configure nginx port] ***************************************************************************************
changed: [192.168.91.132]

RUNNING HANDLER [restart nginx] ***********************************************************************************
changed: [192.168.91.132]

PLAY RECAP ********************************************************************************************************
192.168.91.132             : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
当[configure nginx port]发生改变时,会执行hadlers,RUNNING HANDLER [restart nginx]

handlers是一种任务列表,其中可以有多个任务,被不同的notify触发
---
- hosts: test70
  remote_user: root
  tasks:
  - name: make testfile1
    file: path=/testdir/testfile1
          state=directory
    notify: ht2
  - name: make testfile2
    file: path=/testdir/testfile2
          state=directory
    notify: ht1
 
  handlers:
  - name: ht1
    file: path=/testdir/ht1
          state=touch
  - name: ht2
    file: path=/testdir/ht2
          state=touch

handlers的执行顺序与其在playbook中的第一顺序有关,与notify的顺序无关

在默认情况下,在所有tasks执行完成后,才会执行每个handlers,如要在执行某个task以后立即执行对应的handler,需要用meta模块
---
- hosts: test70
  remote_user: root
  tasks:
  - name: task1
    file: path=/testdir/testfile
          state=touch
    notify: handler1
  - name: task2
    file: path=/testdir/testfile2
          state=touch
    notify: handler2
 
  - meta: flush_handlers
 
  - name: task3
    file: path=/testdir/testfile3
          state=touch
    notify: handler3
 
  handlers:
  - name: handler1
    file: path=/testdir/ht1
          state=touch
  - name: handler2
    file: path=/testdir/ht2
          state=touch
  - name: handler3
    file: path=/testdir/ht3
          state=touch
          
meta: flush_handlers表示当执行完task1和task2后,立即执行对应的handler,而不像等所有task执行完后再执行handler

可以一次性notify多个handler
---
- hosts: test70
  remote_user: root
  tasks:
  - name: task1
    file: path=/testdir/testfile
          state=touch
    notify: handler group1
 
  handlers:
  - name: handler1
    listen: handler group1
    file: path=/testdir/ht1
          state=touch
  - name: handler2
    listen: handler group1
    file: path=/testdir/ht2
          state=touch
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值