title: Create dictionary with content of a list and it's count with Ansible using Jinja2
body: <p>I have a list as below:</p>
  <pre><code>str_lst:
    - &quot;someline    user1     OK     somedata    somecommand&quot;
    - &quot;someline    user1     OK     somedata    somecommand&quot;
    - &quot;someline    user1     OK     somedata    somecommand&quot;
    - &quot;someline    user2     OK     somedata    somecommand&quot;
    - &quot;someline    user2     OK     somedata    somecommand&quot;
  </code></pre>
  <p>I have to create a map with the 'second field from each line' : 'it's count'.<br />
  I have tried with the below Jinja2 code but not sure how to get the total count for each item:</p>
  <pre><code>- 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
  </code></pre>
  <p>Output:</p>
  <pre><code>&quot;users&quot;: {
      &quot;user1&quot;: 1,
      &quot;user2&quot;: 1
  }
  </code></pre>
  <p>Expected output:</p>
  <pre><code>&quot;users&quot;: {
      &quot;user1&quot;: 3,
      &quot;user2&quot;: 2
  }
  </code></pre>
  <p>Any idea, how to achieve this? Thanks in advance</p>