# counting the array of elements in ansible.

**URL:** <https://forum.ansible.com/t/counting-the-array-of-elements-in-ansible/32501>\
**Category:** Ansible Project\
**Created:** [March 19, 2020, 3:23pm UTC](https://forum.ansible.com/t/counting-the-array-of-elements-in-ansible/32501 "2020-03-19T15:23:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahesh\_g\_o](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mahesh\_g\_o](https://forum.ansible.com/u/mahesh_g_o)\
**Post date:** [March 19, 2020, 3:23pm UTC](https://forum.ansible.com/t/counting-the-array-of-elements-in-ansible/32501/1 "2020-03-19T15:23:58Z")

</div>

- hosts: localhost  
gather\_facts: false  
vars:  
foo: [‘vs1’,‘vs2’,‘vs3’,‘vs4’]  
tasks:
- name: counting the array elements  
yes: “{{foo|length}}”

seeing the below error: not sure what am i missing here:

ERROR! Syntax Error while loading YAML.  
mapping values are not allowed in this context

The error appears to be in ‘/root/ansible/playbooks/syst/lab.yml’: line 7, column 12, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: counting the array elements  
yes: “{{foo}}”|length  
^ here  
We could be wrong, but this one looks like it might be an issue with  
missing quotes. Always quote template expression brackets when they  
start a value. For instance:

with\_items:

- {{ foo }}

Should be written as:

with\_items:

- “{{ foo }}”

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 19, 2020, 3:28pm UTC](https://forum.ansible.com/t/counting-the-array-of-elements-in-ansible/32501/2 "2020-03-19T15:28:53Z")

</div>

> - hosts: localhost  
> gather\_facts: false  
> vars:  
> foo: ['vs1','vs2','vs3','vs4']  
> tasks:  
> - name: counting the array elements  
> yes: "{{foo|length}}"

There is no module called "yes", try "set\_fact" or "debug" and make sure your indentation is correct.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![mahesh\_g\_o](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mahesh\_g\_o](https://forum.ansible.com/u/mahesh_g_o)\
**Post date:** [March 19, 2020, 3:56pm UTC](https://forum.ansible.com/t/counting-the-array-of-elements-in-ansible/32501/3 "2020-03-19T15:56:39Z")

</div>

Thanks it worked.

vars:  
foo: [‘vs1’,‘vs2’,‘vs3’,‘vs4’]  
tasks:

- name: counting the array elements  
debug:  
msg: “{{foo|length}}”
