Update some examples to use proper YAML syntax. (#35640)

* Update some examples to use proper YAML syntax.

* Make the requested changes so this builds properly.

Also fix emphasis line numbers to match what was being emphasized before
the change.
pull/35673/head
Joshua Smith 7 years ago committed by John R Barker
parent 3dfede5642
commit 7b76da2d4c

@ -8,19 +8,26 @@ Blocks allow for logical grouping of tasks and in play error handling. Most of w
:emphasize-lines: 3 :emphasize-lines: 3
:caption: Block example :caption: Block example
tasks:
- name: Install Apache
block:
- yum: name={{ item }} state=installed
loop:
- 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
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
In the example above, each of the 3 tasks will be executed after appending the `when` condition from the block In the example above, each of the 3 tasks will be executed after appending the `when` condition from the block
and evaluating it in the task's context. Also they inherit the privilege escalation directives enabling "become to root" and evaluating it in the task's context. Also they inherit the privilege escalation directives enabling "become to root"
@ -38,22 +45,27 @@ Error Handling
Blocks also introduce the ability to handle errors in a way similar to exceptions in most programming languages. Blocks also introduce the ability to handle errors in a way similar to exceptions in most programming languages.
.. code-block:: YAML .. code-block:: YAML
:emphasize-lines: 3,7,11 :emphasize-lines: 3,9,15
:caption: Block error handling example :caption: Block error handling example
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"
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"
The tasks in the ``block`` would execute normally, if there is any error the ``rescue`` section would get executed The tasks in the ``block`` would execute normally, if there is any error the ``rescue`` section would get executed
with whatever you need to do to recover from the previous error. The ``always`` section runs no matter what previous with whatever you need to do to recover from the previous error. The ``always`` section runs no matter what previous
@ -64,21 +76,24 @@ error did or did not occur in the ``block`` and ``rescue`` sections. It should b
Another example is how to run handlers after an error occurred : Another example is how to run handlers after an error occurred :
.. code-block:: YAML .. code-block:: YAML
:emphasize-lines: 5,9 :emphasize-lines: 6,10
:caption: Block run handlers in error handling :caption: Block run handlers in error handling
tasks: tasks:
- name: Attempt and gracefull roll back demo - name: Attempt and gracefull roll back demo
block: block:
- debug: msg='I execute normally' - debug:
msg: 'I execute normally'
notify: run me even after an error notify: run me even after an error
- command: /bin/false - 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: msg='this handler runs even on error' debug:
msg: 'this handler runs even on error'
.. seealso:: .. seealso::

Loading…
Cancel
Save