Transforming ansible list into a dict of ocurrences

Viewed 51
2 Answers

For example

    - set_fact:
        f1: "{{ f1|d([]) + [{item: _len|int}] }}"
      loop: "{{ _unq }}"
      vars:
        _lst: "{{ identifier_1 }}"
        _unq: "{{ _lst|unique|sort }}"
        _len: "{{ _lst|select('eq', item)|length }}"

gives

  f1:
  - 1: 3
  - 2: 1

you could use a custom filter:

you create a folder filter_plugins in your playbook folder (i have named the file myfilters.py and the filter CounterX)

#!/usr/bin/python
class FilterModule(object):
    def filters(self):
        return {
            'CounterX': self.CounterX
        }

    def CounterX(self, obj):
        dicts={}   

        for k in obj.keys():
            mydict = dict((x,obj[k].count(x)) for x in set(obj[k]))
            dicts[k] = mydict

        return dicts

then you use it in your playbook:

- name: vartest
  hosts: localhost
  vars:
    variable:
      identifier_1:
        - 1
        - 2
        - 1
        - 1
      identifier_2:
        - 1
        - 2
        - 3
  tasks:
    - name: transform value
      set_fact:
        result: "{{ variable | CounterX }}"

    - name: display result
      debug:
        var: result

result:

ok: [localhost] => {
    "result": {
        "identifier_1": {
            "1": 3,
            "2": 1
        },
        "identifier_2": {
            "1": 1,
            "2": 1,
            "3": 1
        }
    }
}
Related