0%

Ansible-part12: block rescue and always

Blocks

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
anna@ansible-controller:~/Desktop/ansible-code/inventory/block-resuce-always$ ansible-playbook -i hosts site.yml 

PLAY [test blocks resuce and always] ******************************************************************************************************************************************************************************************************************

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

TASK [first command] **********************************************************************************************************************************************************************************************************************************
ok: [cube4200] => {
"msg": "first message sucess"
}

TASK [second command] *********************************************************************************************************************************************************************************************************************************
fatal: [cube4200]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.011635", "end": "2020-06-21 21:31:18.222247", "msg": "non-zero return code", "rc": 1, "start": "2020-06-21 21:31:18.210612", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************
cube4200 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

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"

after running

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
anna@ansible-controller:~/Desktop/ansible-code/inventory/block-resuce-always$ ansible-playbook -i hosts site.yml 

PLAY [test blocks resuce and always] ******************************************************************************************************************************************************************************************************************

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

TASK [test block, this is first command] **************************************************************************************************************************************************************************************************************
ok: [cube4200] => {
"msg": "first message success"
}

TASK [second command] *********************************************************************************************************************************************************************************************************************************
fatal: [cube4200]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.010445", "end": "2020-06-21 21:42:37.100618", "msg": "non-zero return code", "rc": 1, "start": "2020-06-21 21:42:37.090173", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

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"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************
cube4200 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
anna@ansible-controller:~/Desktop/ansible-code/inventory/block-resuce-always$ ansible-playbook -i hosts site.yml 

PLAY [test blocks resuce and always] ******************************************************************************************************************************************************************************************************************

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

TASK [test block, this is first command] **************************************************************************************************************************************************************************************************************
ok: [cube4200] => {
"msg": "first message success"
}

TASK [second command] *********************************************************************************************************************************************************************************************************************************
changed: [cube4200]

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************
cube4200 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
anna@ansible-controller:~/Desktop/ansible-code/inventory/block-resuce-always$ ansible-playbook -i hosts site.yml 

PLAY [test blocks resuce and always] ******************************************************************************************************************************************************************************************************************

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

TASK [test block, this is first command] **************************************************************************************************************************************************************************************************************
ok: [cube4200] => {
"msg": "first message success"
}

TASK [second command] *********************************************************************************************************************************************************************************************************************************
changed: [cube4200]

TASK [test always. whatever this task have to be executed.] *******************************************************************************************************************************************************************************************
ok: [cube4200] => {
"msg": "this section definitely be executed!!"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************
cube4200 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

last 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/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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
anna@ansible-controller:~/Desktop/ansible-code/inventory/block-resuce-always$ ansible-playbook -i hosts site.yml 

PLAY [test blocks resuce and always] ******************************************************************************************************************************************************************************************************************

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

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!!"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************
cube4200 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0