Counting elements in a sequence

The community.general.counter filter plugin allows you to count (hashable) elements in a sequence. Elements are returned as dictionary keys and their counts are stored as dictionary values.

- name: Count character occurrences in a string
  debug:
    msg: "{{ 'abccbaabca' | community.general.counter }}"

- name: Count items in a list
  debug:
    msg: "{{ ['car', 'car', 'bike', 'plane', 'bike'] | community.general.counter }}"

This produces:

TASK [Count character occurrences in a string] ********************************************
ok: [localhost] => {
    "msg": {
        "a": 4,
        "b": 3,
        "c": 3
    }
}

TASK [Count items in a list] **************************************************************
ok: [localhost] => {
    "msg": {
        "bike": 2,
        "car": 2,
        "plane": 1
    }
}

This plugin is useful for selecting resources based on current allocation:

- name: Get ID of SCSI controller(s) with less than 4 disks attached and choose the one with the least disks
  debug:
    msg: >-
      {{
         ( disks | dict2items | map(attribute='value.adapter') | list
           | community.general.counter | dict2items
           | rejectattr('value', '>=', 4) | sort(attribute='value') | first
         ).key
      }}
  vars:
    disks:
      sda:
        adapter: scsi_1
      sdb:
        adapter: scsi_1
      sdc:
        adapter: scsi_1
      sdd:
        adapter: scsi_1
      sde:
        adapter: scsi_2
      sdf:
        adapter: scsi_3
      sdg:
        adapter: scsi_3

This produces:

TASK [Get ID of SCSI controller(s) with less than 4 disks attached and choose the one with the least disks]
ok: [localhost] => {
    "msg": "scsi_2"
}

New in version 4.3.0.