Output only changed list items

My vars.yml file is:

`
linux_users:

  • username: testuser1
    state: present

  • username: testuser2
    state: present

`

If I add another user:

`
linux_users:

  • username: testuser1
    state: present

  • username: testuser2
    state: present

  • username: testuser3
    state: present
    `

how can I output only the username of the third user who was created? My task file is:

`

  • hosts: 127.0.0.1
    become: True
    vars_files:

  • vars.yml

  • name: Create user
    user:
    name: “{{ item.username }}”
    state: ‘{{ item.state }}’
    register: user
    when: item.state != ‘absent’
    with_items: ‘{{ linux_users }}’

  • name: Output username and password
    debug:
    msg: “User was created: {{ item }}”
    with_items: ‘{{ user.results[2].name }}’
    when: user.changed
    `

{{ user.results[2].name }} outputs what I need, but I won’t know which user will be created when I run the script. Is there any way to register the list index integer of the user that was created?

I believe you want this:

- name: Output username and password
    debug:
      msg: "User was created: {{ item.name }}"
    with_items: '{{ user.results }}'
    when: item|changed