Create dictionary with content of a list and it's count with Ansible using Jinja2

0

I have a list as below:

str_lst:
  - "someline    user1     OK     somedata    somecommand"
  - "someline    user1     OK     somedata    somecommand"
  - "someline    user1     OK     somedata    somecommand"
  - "someline    user2     OK     somedata    somecommand"
  - "someline    user2     OK     somedata    somecommand"

I have to create a map with the 'second field from each line' : 'it's count'.
I have tried with the below Jinja2 code but not sure how to get the total count for each item:

- set_fact:
    users: |-
      {%- set users = [] -%}
      {%- set u_dict = {} -%}
      {%- for i in str_lst -%}
         {{ users.append(i.split()[1]) }}
      {%- endfor -%}
      {%- for j in users -%}
        {{ u_dict.update({j:j.count(j)}) }}
      {%- endfor -%}
      {{ u_dict }}

- debug: var=users

Output:

"users": {
    "user1": 1,
    "user2": 1
}

Expected output:

"users": {
    "user1": 3,
    "user2": 2
}

Any idea, how to achieve this? Thanks in advance

ansible jinja2
2021-11-23 15:08:53
1

1

you could use a custom filter named listcount for example:

create a folder named filter_plugins in your playbook folder and create a file named customfilter.py:

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

    def listcount(self, listvar):
        result = {}
        for l in listvar:
            item = l.split()[1]
            if item in result:
                result[item] += 1
            else:
                result[item] = 1

        return result

playbook:

- hosts: localhost
  vars:
    str_lst:
      - "someline    user1     OK     somedata    somecommand"
      - "someline    user1     OK     somedata    somecommand"
      - "someline    user1     OK     somedata    somecommand"
      - "someline    user2     OK     somedata    somecommand"
      - "someline    user2     OK     somedata    somecommand"
  tasks:
        
    - set_fact:
        users: "{{ str_lst | listcount }}"
    - debug:
        msg: "{{ users }}"

result:

ok: [localhost] => {
    "msg": {
        "user1": 3,
        "user2": 2
    }
}

without customfilter, i have tested this jinja2 code and it seems ok:

  tasks:
    - set_fact:
        users: |-
          {%- set dict = {} -%}
          {%- for l in str_lst -%}
          {%- set i = l.split()[1] -%}
          {%-  if i in dict -%}
          {%- set _ = dict.update({i: dict[i] + 1}) -%}      
          {%- else -%}
          {%- set _ = dict.update({i: 1}) -%}
          {%- endif -%}        
          {%- endfor -%}
          {{ dict }}
    - debug:
        msg: "{{ users }}"
    - debug:
        msg: "{{ users.user1 }}  {{ users.user2 }}"
2021-11-23 16:47:40

That worked well. Thank you!
Vijesh

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................