ansible任务控制-错误处理changed_when
force_handlers:yes
强制执行handlers
默认情况下,当task失败后,play会终止,后续的handlers就不会被执行,可是使用参数 force_handlers:yes
强制执行handlers
编辑yml文件
cat >tasks_changed_when.yml << EOF
- hosts: all
tasks:
- name: touch test file
file:
path: /tmp/test.txt
state: touch
notify: restart httpd
- name: exec error command
shell: abc
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
EOF
执行yml文件
可以看到,当我们执行一个错误的命令abc后,后边重启httpd的handlers没有被执行
$ ansible-playbook tasks_changed_when.yml
PLAY [all] **************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [devops02]
ok: [devops01]
TASK [touch test file] **************************************************************************************************************
changed: [devops02]
changed: [devops01]
TASK [exec error command] ***********************************************************************************************************
fatal: [devops02]: FAILED! => {"changed": true, "cmd": "abc", "delta": "0:00:00.087827", "end": "2021-09-25 15:28:19.873285", "msg": "non-zero return code", "rc": 127, "start": "2021-09-25 15:28:19.785458", "stderr": "/bin/sh: abc: command not found", "stderr_lines": ["/bin/sh: abc: command not found"], "stdout": "", "stdout_lines": []}
fatal: [devops01]: FAILED! => {"changed": true, "cmd": "abc", "delta": "0:00:00.087584", "end": "2021-09-25 15:28:19.941478", "msg": "non-zero return code", "rc": 127, "start": "2021-09-25 15:28:19.853894", "stderr": "/bin/sh: abc: command not found", "stderr_lines": ["/bin/sh: abc: command not found"], "stdout": "", "stdout_lines": []}
RUNNING HANDLER [restart httpd] *****************************************************************************************************
PLAY RECAP **************************************************************************************************************************
devops01 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
devops02 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
在yml文件中添加参数 force_handlers: yes
强制执行handlers
cat >tasks_changed_when.yml << EOF
- hosts: all
force_handlers: yes
tasks:
- name: touch test file
file:
path: /tmp/test.txt
state: touch
notify: restart httpd
- name: exec error command
shell: abc
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
EOF
再次执行yml文件,可以看到handlers被强制执行了
$ ansible-playbook tasks_changed_when.yml
PLAY [all] **************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [devops01]
ok: [devops02]
TASK [touch test file] **************************************************************************************************************
changed: [devops02]
changed: [devops01]
TASK [exec error command] ***********************************************************************************************************
fatal: [devops01]: FAILED! => {"changed": true, "cmd": "abc", "delta": "0:00:00.080787", "end": "2021-09-25 15:34:15.667841", "msg": "non-zero return code", "rc": 127, "start": "2021-09-25 15:34:15.587054", "stderr": "/bin/sh: abc: command not found", "stderr_lines": ["/bin/sh: abc: command not found"], "stdout": "", "stdout_lines": []}
fatal: [devops02]: FAILED! => {"changed": true, "cmd": "abc", "delta": "0:00:00.078859", "end": "2021-09-25 15:34:15.747691", "msg": "non-zero return code", "rc": 127, "start": "2021-09-25 15:34:15.668832", "stderr": "/bin/sh: abc: command not found", "stderr_lines": ["/bin/sh: abc: command not found"], "stdout": "", "stdout_lines": []}
RUNNING HANDLER [restart httpd] *****************************************************************************************************
changed: [devops01]
changed: [devops02]
PLAY RECAP **************************************************************************************************************************
devops01 : ok=3 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
devops02 : ok=3 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0