counting the array of elements in ansible.

91 views
Skip to first unread message

mahesh g o

unread,
Mar 19, 2020, 11:23:58 AM3/19/20
to Ansible Project
- 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 }}"


Stefan Hornburg (Racke)

unread,
Mar 19, 2020, 11:29:24 AM3/19/20
to ansible...@googlegroups.com
On 3/19/20 4:23 PM, mahesh g o wrote:
> - 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
Racke

>
> 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 }}"
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/5e834a5d-2bb7-4875-b773-591c9a20a6da%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/5e834a5d-2bb7-4875-b773-591c9a20a6da%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

mahesh g o

unread,
Mar 19, 2020, 11:56:39 AM3/19/20
to Ansible Project
Thanks  it worked.

vars:
    foo: ['vs1','vs2','vs3','vs4']
  tasks:
    - name: counting the array elements
      debug:
         msg: "{{foo|length}}"
Reply all
Reply to author
Forward
0 new messages