Docsite: fix user_guide/playbooks_blocks (#71769)
This commit is contained in:
parent
1cf42897d2
commit
79dc6fa948
1 changed files with 55 additions and 31 deletions
|
@ -21,18 +21,20 @@ All tasks in a block inherit directives applied at the block level. Most of what
|
||||||
tasks:
|
tasks:
|
||||||
- name: Install, configure, and start Apache
|
- name: Install, configure, and start Apache
|
||||||
block:
|
block:
|
||||||
- name: install httpd and memcached
|
- name: Install httpd and memcached
|
||||||
yum:
|
ansible.builtin.yum:
|
||||||
name:
|
name:
|
||||||
- httpd
|
- httpd
|
||||||
- memcached
|
- memcached
|
||||||
state: present
|
state: present
|
||||||
- name: apply the foo config template
|
|
||||||
template:
|
- name: Apply the foo config template
|
||||||
|
ansible.builtin.template:
|
||||||
src: templates/src.j2
|
src: templates/src.j2
|
||||||
dest: /etc/foo.conf
|
dest: /etc/foo.conf
|
||||||
- name: start service bar and enable it
|
|
||||||
service:
|
- name: Start service bar and enable it
|
||||||
|
ansible.builtin.service:
|
||||||
name: bar
|
name: bar
|
||||||
state: started
|
state: started
|
||||||
enabled: True
|
enabled: True
|
||||||
|
@ -62,14 +64,19 @@ Rescue blocks specify tasks to run when an earlier task in a block fails. This a
|
||||||
tasks:
|
tasks:
|
||||||
- name: Handle the error
|
- name: Handle the error
|
||||||
block:
|
block:
|
||||||
- debug:
|
- name: Print a message
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'I execute normally'
|
msg: 'I execute normally'
|
||||||
- name: i force a failure
|
|
||||||
command: /bin/false
|
- name: Force a failure
|
||||||
- debug:
|
ansible.builtin.command: /bin/false
|
||||||
|
|
||||||
|
- name: Never print this
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'I never execute, due to the above task failing, :-('
|
msg: 'I never execute, due to the above task failing, :-('
|
||||||
rescue:
|
rescue:
|
||||||
- debug:
|
- name: Print when errors
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'I caught an error, can do stuff here to fix it, :-)'
|
msg: 'I caught an error, can do stuff here to fix it, :-)'
|
||||||
|
|
||||||
You can also add an ``always`` section to a block. Tasks in the ``always`` section run no matter what the task status of the previous block is.
|
You can also add an ``always`` section to a block. Tasks in the ``always`` section run no matter what the task status of the previous block is.
|
||||||
|
@ -81,14 +88,19 @@ You can also add an ``always`` section to a block. Tasks in the ``always`` secti
|
||||||
|
|
||||||
- name: Always do X
|
- name: Always do X
|
||||||
block:
|
block:
|
||||||
- debug:
|
- name: Print a message
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'I execute normally'
|
msg: 'I execute normally'
|
||||||
- name: i force a failure
|
|
||||||
command: /bin/false
|
- name: Force a failure
|
||||||
- debug:
|
ansible.builtin.command: /bin/false
|
||||||
|
|
||||||
|
- name: Never print this
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'I never execute :-('
|
msg: 'I never execute :-('
|
||||||
always:
|
always:
|
||||||
- debug:
|
- name: Always do this
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: "This always executes, :-)"
|
msg: "This always executes, :-)"
|
||||||
|
|
||||||
Together, these elements offer complex error handling.
|
Together, these elements offer complex error handling.
|
||||||
|
@ -99,26 +111,35 @@ Together, these elements offer complex error handling.
|
||||||
|
|
||||||
- name: Attempt and graceful roll back demo
|
- name: Attempt and graceful roll back demo
|
||||||
block:
|
block:
|
||||||
- debug:
|
- name: Print a message
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'I execute normally'
|
msg: 'I execute normally'
|
||||||
- name: i force a failure
|
|
||||||
command: /bin/false
|
- name: Force a failure
|
||||||
- debug:
|
ansible.builtin.command: /bin/false
|
||||||
|
|
||||||
|
- name: Never print this
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'I never execute, due to the above task failing, :-('
|
msg: 'I never execute, due to the above task failing, :-('
|
||||||
rescue:
|
rescue:
|
||||||
- debug:
|
- name: Print when errors
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'I caught an error'
|
msg: 'I caught an error'
|
||||||
- name: i force a failure in middle of recovery! >:-)
|
|
||||||
command: /bin/false
|
- name: Force a failure in middle of recovery! >:-)
|
||||||
- debug:
|
ansible.builtin.command: /bin/false
|
||||||
|
|
||||||
|
- name: Never print this
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'I also never execute :-('
|
msg: 'I also never execute :-('
|
||||||
always:
|
always:
|
||||||
- debug:
|
- name: Always do this
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: "This always executes"
|
msg: "This always executes"
|
||||||
|
|
||||||
The tasks in the ``block`` execute normally. If any tasks in the block return ``failed``, the ``rescue`` section executes tasks to recover from the error. The ``always`` section runs regardless of the results of the ``block`` and ``rescue`` sections.
|
The tasks in the ``block`` execute normally. If any tasks in the block return ``failed``, the ``rescue`` section executes tasks to recover from the error. The ``always`` section runs regardless of the results of the ``block`` and ``rescue`` sections.
|
||||||
|
|
||||||
If an error occurs in the block and the rescue task succeeds, Ansible reverts the failed status of the original task for the run and continues to run the play as if the original task had succeeded. The rescued task is considered successful, and does not not trigger ``max_fail_percentage`` or ``any_errors_fatal`` configurations. However, Ansible still reports a failure in the playbook statistics.
|
If an error occurs in the block and the rescue task succeeds, Ansible reverts the failed status of the original task for the run and continues to run the play as if the original task had succeeded. The rescued task is considered successful, and does not trigger ``max_fail_percentage`` or ``any_errors_fatal`` configurations. However, Ansible still reports a failure in the playbook statistics.
|
||||||
|
|
||||||
You can use blocks with ``flush_handlers`` in a rescue task to ensure that all handlers run even if an error occurs:
|
You can use blocks with ``flush_handlers`` in a rescue task to ensure that all handlers run even if an error occurs:
|
||||||
|
|
||||||
|
@ -129,17 +150,20 @@ You can use blocks with ``flush_handlers`` in a rescue task to ensure that all h
|
||||||
tasks:
|
tasks:
|
||||||
- name: Attempt and graceful roll back demo
|
- name: Attempt and graceful roll back demo
|
||||||
block:
|
block:
|
||||||
- debug:
|
- name: Print a message
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'I execute normally'
|
msg: 'I execute normally'
|
||||||
changed_when: yes
|
changed_when: yes
|
||||||
notify: run me even after an error
|
notify: run me even after an error
|
||||||
- command: /bin/false
|
|
||||||
|
- name: Force a failure
|
||||||
|
ansible.builtin.command: /bin/false
|
||||||
rescue:
|
rescue:
|
||||||
- name: make sure all handlers run
|
- name: Make sure all handlers run
|
||||||
meta: flush_handlers
|
meta: flush_handlers
|
||||||
handlers:
|
handlers:
|
||||||
- name: run me even after an error
|
- name: Run me even after an error
|
||||||
debug:
|
ansible.builtin.debug:
|
||||||
msg: 'This handler runs even on error'
|
msg: 'This handler runs even on error'
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue