community.general.counter filter – Counts hashable elements in a sequence

Note

This filter plugin is part of the community.general collection (version 8.6.8).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.general.

To use it in a playbook, specify: community.general.counter.

New in community.general 4.3.0

Synopsis

  • Counts hashable elements in a sequence.

Input

This describes the input of the filter, the value before | community.general.counter.

Parameter

Comments

Input

list / elements=any / required

A sequence.

Examples

- name: Count occurrences
  ansible.builtin.debug:
    msg: >-
      {{ [1, 'a', 2, 2, 'a', 'b', 'a'] | community.general.counter }}
    # Produces: {1: 1, 'a': 3, 2: 2, 'b': 1}

Return Value

Key

Description

Return value

dictionary

A dictionary with the elements of the sequence as keys, and their number of occurrences in the sequence as values.

Returned: success

Authors

  • Rémy Keil (@keilr)

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.