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)调用方式
- copy:
[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
关注的资源发生变化时采取的操作
- 使用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:
- restart httpd
- 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
- copy: