counting the array of elements in ansible.

  • 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 }}”

- 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

Thanks it worked.

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

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