handlers练习

3.1 问题
本案例要求:

安装Apache软件
配置文件,重新载入配置文件让服务生效
使用handlers来实现
3.2 步骤
实现此案例需要按照如下步骤进行。

步骤一:error

playbook从上往下顺序执行,若报错,后面的命令不会在执行,若想解决可以使用ignoring_errors:True(使用这个,会有报错信息,告诉你错误忽略,继续执行下面的命令)

[root@ansible ansible]# vim error.yml

  • hosts: web
    remote_user: root
    tasks:
    • shell: mkdir /tmp/cache
    • name: ReStart service httpd
      service:
      name: httpd
      state: restarted
    • name: run some command
      shell: /usr/bin/somecommand
      ignore_errors: True
      [root@ansible ansible]# ansible-playbook error.yml
      PLAY [web] ***********************************************************************************
      TASK [Gathering Facts] ***********************************************************************************
      ok: [web2]
      ok: [web1]
      TASK [command] ***********************************************************************************
      [WARNING]: Consider using file module with state=directory rather than running mkdir
      fatal: [web1]: FAILED! => {“changed”: true, “cmd”: “mkdir /tmp/cache”, “delta”: “0:00:00.004799”, “end”: “2019-04-11 14:57:11.582036”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2019-04-11 14:57:11.577237”, “stderr”: “mkdir: \u65e0\u6cd5\u521b\u5efa\u76ee\u5f55”/tmp/cache": \u6587\u4ef6\u5df2\u5b58\u5728", “stderr_lines”: [“mkdir: \u65e0\u6cd5\u521b\u5efa\u76ee\u5f55”/tmp/cache": \u6587\u4ef6\u5df2\u5b58\u5728"], “stdout”: “”, “stdout_lines”: []}
      changed: [web2]
      TASK [ReStart service httpd] *****************************************************************************
      fatal: [web2]: FAILED! => {“changed”: false, “msg”: “Could not find the requested service httpd: host”}
      to retry, use: --limit @/etc/ansible/error.retry
      PLAY RECAP ***********************************************************************************
      web1 : ok=1 changed=0 unreachable=0 failed=1
      web2 : ok=2 changed=1 unreachable=0 failed=1
      [root@ansible ansible]#
      步骤二:tags给指定的任务定义一个调用标识

1)tags 样例

[root@ansible ansible]# vim adhttp.yml

  • hosts: cache
    remote_user: root
    tasks:
    • copy:
      src: /root/httpd.conf
      dest: /etc/httpd/conf/httpd.conf
      owner: apache
      group: apache
      mode: 0644
      tags: syncconf
      2)调用方式

[root@ansible ansible]# ansible-playbook tags.yml -t syncconf
PLAY [cache] ***********************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [cache]
TASK [copy] ***********************************************************************************
changed: [cache]
PLAY RECAP ***********************************************************************************
cache : ok=2 changed=1 unreachable=0 failed=0
步骤三: handlers

关注的资源发生变化时采取的操作

  1. 使用handlers来配置文件,重新载入配置文件让服务生效

[root@ansible ansible]# vim handers.yml

  • hosts: cache
    remote_user: root
    tasks:
    • copy:
      src: /root/httpd.conf
      dest: /etc/httpd/conf/httpd.conf
      owner: apache
      group: apache
      mode: 0644
      tags: syncconf
      notify:
      • restart httpd
        handlers:
    • name: restart httpd
      service:
      name: httpd
      state: restarted
      [root@ansible ansible]# ansible-playbook handers.yml
      PLAY [cache] ***********************************************************************************
      TASK [Gathering Facts] ***********************************************************************************
      ok: [cache]
      TASK [copy] ***********************************************************************************
      ok: [cache]
      PLAY RECAP ***********************************************************************************
      cache : ok=2 changed=0 unreachable=0 failed=0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭淦淦

是你的双手成就了我的生活!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值