mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.4 KiB
ReStructuredText
111 lines
3.4 KiB
ReStructuredText
9 years ago
|
Blocks
|
||
|
======
|
||
|
|
||
7 years ago
|
Blocks allow for logical grouping of tasks and in play error handling. Most of what you can apply to a single task can be applied at the block level, which also makes it much easier to set data or directives common to the tasks. This does not mean the directive affects the block itself, but is inherited by the tasks enclosed by a block. i.e. a `when` will be applied to the tasks, not the block itself.
|
||
9 years ago
|
|
||
|
|
||
9 years ago
|
.. code-block:: YAML
|
||
7 years ago
|
:emphasize-lines: 3
|
||
9 years ago
|
:caption: Block example
|
||
9 years ago
|
|
||
|
|
||
7 years ago
|
tasks:
|
||
|
- name: Install Apache
|
||
|
block:
|
||
|
- yum:
|
||
|
name: "{{ item }}"
|
||
|
state: installed
|
||
|
with_items:
|
||
|
- httpd
|
||
|
- memcached
|
||
|
- template:
|
||
|
src: templates/src.j2
|
||
|
dest: /etc/foo.conf
|
||
|
- service:
|
||
|
name: bar
|
||
|
state: started
|
||
|
enabled: True
|
||
|
when: ansible_distribution == 'CentOS'
|
||
|
become: true
|
||
|
become_user: root
|
||
9 years ago
|
|
||
8 years ago
|
In the example above, each of the 3 tasks will be executed after appending the `when` condition from the block
|
||
9 years ago
|
and evaluating it in the task's context. Also they inherit the privilege escalation directives enabling "become to root"
|
||
|
for all the enclosed tasks.
|
||
9 years ago
|
|
||
7 years ago
|
.. versionadded:: 2.3
|
||
7 years ago
|
|
||
7 years ago
|
The ``name:`` keyword for ``block:`` was added in Ansible 2.3.
|
||
9 years ago
|
|
||
|
.. _block_error_handling:
|
||
|
|
||
|
Error Handling
|
||
|
``````````````
|
||
|
|
||
9 years ago
|
Blocks also introduce the ability to handle errors in a way similar to exceptions in most programming languages.
|
||
|
|
||
|
.. code-block:: YAML
|
||
7 years ago
|
:emphasize-lines: 3,9,15
|
||
9 years ago
|
:caption: Block error handling example
|
||
9 years ago
|
|
||
|
|
||
7 years ago
|
tasks:
|
||
|
- name: Attempt and gracefull roll back demo
|
||
|
block:
|
||
|
- debug:
|
||
|
msg: 'I execute normally'
|
||
|
- command: /bin/false
|
||
|
- debug:
|
||
|
msg: 'I never execute, due to the above task failing'
|
||
|
rescue:
|
||
|
- debug:
|
||
|
msg: 'I caught an error'
|
||
|
- command: /bin/false
|
||
|
- debug:
|
||
|
msg: 'I also never execute :-('
|
||
|
always:
|
||
|
- debug:
|
||
|
msg: "this always executes"
|
||
9 years ago
|
|
||
|
The tasks in the ``block`` would execute normally, if there is any error the ``rescue`` section would get executed
|
||
9 years ago
|
with whatever you need to do to recover from the previous error. The ``always`` section runs no matter what previous
|
||
7 years ago
|
error did or did not occur in the ``block`` and ``rescue`` sections. It should be noted that the play continues if a
|
||
|
``rescue`` section completes successfully as it 'erases' the error status (but not the reporting), this means it won't trigger ``max_fail_percentage`` nor ``any_errors_fatal`` configurations but will appear in the playbook statistics.
|
||
9 years ago
|
|
||
|
|
||
9 years ago
|
Another example is how to run handlers after an error occurred :
|
||
|
|
||
|
.. code-block:: YAML
|
||
7 years ago
|
:emphasize-lines: 6,10
|
||
9 years ago
|
:caption: Block run handlers in error handling
|
||
|
|
||
7 years ago
|
|
||
9 years ago
|
tasks:
|
||
7 years ago
|
- name: Attempt and gracefull roll back demo
|
||
|
block:
|
||
7 years ago
|
- debug:
|
||
|
msg: 'I execute normally'
|
||
7 years ago
|
notify: run me even after an error
|
||
|
- command: /bin/false
|
||
|
rescue:
|
||
|
- name: make sure all handlers run
|
||
|
meta: flush_handlers
|
||
7 years ago
|
handlers:
|
||
7 years ago
|
- name: run me even after an error
|
||
7 years ago
|
debug:
|
||
|
msg: 'this handler runs even on error'
|
||
9 years ago
|
|
||
|
.. seealso::
|
||
|
|
||
|
:doc:`playbooks`
|
||
|
An introduction to playbooks
|
||
8 years ago
|
:doc:`playbooks_reuse_roles`
|
||
9 years ago
|
Playbook organization by roles
|
||
|
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||
|
Have a question? Stop by the google group!
|
||
|
`irc.freenode.net <http://irc.freenode.net>`_
|
||
|
#ansible IRC chat channel
|
||
|
|
||
|
|
||
|
|