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