# Need help about Ansible block syntax

**URL:** <https://forum.ansible.com/t/need-help-about-ansible-block-syntax/26614>\
**Category:** Ansible Project\
**Created:** [January 31, 2018, 3:29pm UTC](https://forum.ansible.com/t/need-help-about-ansible-block-syntax/26614 "2018-01-31T15:29:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JiElPe-Fr38](https://avatars.discourse-cdn.com/v4/letter/j/85f322/32.png) [@JiElPe-Fr38](https://forum.ansible.com/u/JiElPe-Fr38)\
**Post date:** [January 31, 2018, 3:29pm UTC](https://forum.ansible.com/t/need-help-about-ansible-block-syntax/26614/1 "2018-01-31T15:29:00Z")

</div>

Dear all,

I am quite new to Ansible and try to “make a tour”…  
I am using ansible 2.4.2.0 with cygwin64 on Win7.

I am currently make a personal training and find that I could use a block to “enclose” (ok, perhaps wrong term) a couple of task that have some common “properties” (like become\_user: root …).  
The idea is to change this :

`

`

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 31, 2018, 4:48pm UTC](https://forum.ansible.com/t/need-help-about-ansible-block-syntax/26614/2 "2018-01-31T16:48:58Z")

</div>

block does not support ALL the features of a simple task so this may be your issue. For example, it may not support notify, but you’ll need to look at this.

---

<div class="post-metadata">

**Author:** ![Malcolm\_Hussain-Gamb](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Malcolm\_Hussain-Gamb](https://forum.ansible.com/u/Malcolm_Hussain-Gamb)\
**Post date:** [January 31, 2018, 10:29pm UTC](https://forum.ansible.com/t/need-help-about-ansible-block-syntax/26614/3 "2018-01-31T22:29:47Z")

</div>

Notify should go after each of the tasks.  
You could set a fact in the block then check that and notify afterwards though

---

<div class="post-metadata">

**Author:** ![JiElPe-Fr38](https://avatars.discourse-cdn.com/v4/letter/j/85f322/32.png) [@JiElPe-Fr38](https://forum.ansible.com/u/JiElPe-Fr38)\
**Post date:** [February 1, 2018, 10:32am UTC](https://forum.ansible.com/t/need-help-about-ansible-block-syntax/26614/4 "2018-02-01T10:32:36Z")

</div>

Thanks all for your help.  
I have been really bored with the block… but I finally succeed. The ansible / yaml syntax is not always straightforward to understand (or I lack some skills).

I went back to the [ansible block documentation](https://docs.ansible.com/ansible/latest/playbooks_blocks.html), and make a trial replacing my main.yml file with the example on the page, i.e. :

`  
cat roles/devTools/tasks/main.yml  
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

`

And I still get an error saying that the role must contain a list of task :

`  
ERROR! The tasks/main.yml file for role ‘devTools’ must contain a list of tasks

The error appears to have been in ‘/home/jeanlupi/centos\_test/myKnowledge/devOps/playbook/roles/devTools/tasks/main.yml’: line 1, column 1, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

tasks:  
^ here

`

Ok… I removed “tasks” (don’t clearly understand why it must not be there…) and the problem disappeared.  
After several other syntax errors (probably not totally sticked to block), I succeeded with :

`  
cat roles/devTools/tasks/RedHat.yml

- name: Update Red Hat machine  
block:
- yum: update\_cache=yes name=“\*” state=latest  
notify: “restart {{ ansible\_os\_family }} host”
- yum: name=“{{item}}” state=present  
with\_items: “{{ toolList }}”  
`notify: "restart {{ ansible_os_family }} host"`  
when: ansible\_pkg\_mgr == ‘yum’  
become: true  
become\_user: root

`

As mentioned in the answers, notify must not be set at block level, but after each enclosed task.

Thanks for your help.  
Have a nice day.  
JiElPe

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 19, 2018, 9:19pm UTC](https://forum.ansible.com/t/need-help-about-ansible-block-syntax/26614/5 "2018-02-19T21:19:44Z")

</div>

'tasks' is a property of a play, it should ONLY appear at the play level
