Blocks allow for logical grouping of tasks and in play error handling.
1 2 3 4 5 6 7 8 9 10 11
- name: test blocks resuce and always hosts: web2 tasks: - name: first command debug: msg: "first message success" - name: second command command: /bin/false - name: third command debug: msg: "this is third command"
There are three tasks in this playbook, but the second one is a wrong command, which will failed to be executed.
PLAY [test blocks resuce and always] ******************************************************************************************************************************************************************************************************************
Since the second faild, so the third one will be skipped. I don’t want this happen, So I use block to solove this.
Blocks error handling
Blocks also introduce the ability to handle errors in a way similar to exceptions in most programming languages. Blocks only deal with ‘failed’ status of a task. A bad task definition or an unreachable host are not ‘rescuable’ errors.
three key words: block rescure always are similar as try exception and final in python. block section: will execute normally rescue section: will execute only when block section has error always section: will run no matter what the task status is.
I add block into playbook
1 2 3 4 5 6 7 8 9 10 11 12 13
- name: test blocks recue and always hosts: web2 tasks: - block: - name: test block, this is first command debug: msg: "first message success" - name: second command command: /bin/false rescue: - name: third command, this is excuted only when block failed, otherwise will be skipped debug: msg: "some command in block failed, so this message be excuted"
PLAY [test blocks resuce and always] ******************************************************************************************************************************************************************************************************************
TASK [third command, this is excuted only when block failed, otherwise will be skipped] *************************************************************************************************************************************************************** ok: [cube4200] => { "msg": "some command in block failed, so this message be excuted" }
eventhough the second command failed, the third command still be excuted since it is in the rescue section.
I am testing another situation
1 2 3 4 5 6 7 8 9 10 11
tasks: - block: - name: test block, this is first command debug: msg: "first message success" - name: second command command: /bin/date rescue: - name: third command, this is excuted only when block failed, otherwise will be skipped debug: msg: "some command in block failed, so this message be excuted"
In this playbook, the second command is correct. what will happed to rescue section?
PLAY [test blocks resuce and always] ******************************************************************************************************************************************************************************************************************
It shows the rescue section isn’t be excuted any more.
Let’s look at always scenario
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
tasks: - block: - name: test block, this is first command debug: msg: "first message success" - name: second command command: /bin/date rescue: - name: third command, this is excuted only when block failed, otherwise will be skipped debug: msg: "some command in block failed, so this message be excuted" always: - name: test always. whatever this task have to be executed. debug: msg: "this section definitely be executed!!"
whatever happen, the always section will be executed, but rescue section isn’t excuted since the second command successed.
PLAY [test blocks resuce and always] ******************************************************************************************************************************************************************************************************************
TASK [test always. whatever this task have to be executed.] ******************************************************************************************************************************************************************************************* ok: [cube4200] => { "msg": "this section definitely be executed!!" }
tasks: - block: - name: test block, this is first command debug: msg: "first message success" - name: second command command: /bin/da rescue: - name: third command, this is excuted only when block failed, otherwise will be skipped debug: msg: "some command in block failed, so this message be excuted" always: - name: test always. whatever this task have to be executed. debug: msg: "this section definitely be executed!!"
the second command failed, both rescue and always will be excuted.
PLAY [test blocks resuce and always] ******************************************************************************************************************************************************************************************************************
TASK [test block, this is first command] ************************************************************************************************************************************************************************************************************** ok: [cube4200] => { "msg": "first message success" }
TASK [second command] ********************************************************************************************************************************************************************************************************************************* fatal: [cube4200]: FAILED! => {"changed": false, "cmd": "/bin/da", "msg": "[Errno 2] No such file or directory", "rc": 2}
TASK [third command, this is excuted only when block failed, otherwise will be skipped] *************************************************************************************************************************************************************** ok: [cube4200] => { "msg": "some command in block failed, so this message be excuted" }
TASK [test always. whatever this task have to be executed.] ******************************************************************************************************************************************************************************************* ok: [cube4200] => { "msg": "this section definitely be executed!!" }