Outputting only changed list items

I have a vars.yml with a list of users:

`
linux_users:

  • username: testuser1
    state: present

  • username: testuser2
    state: present
    `

If I create them, then add a third:

`
linux_users:

  • username: testuser1
    state: present

  • username: testuser2
    state: present

  • username: testuser3
    state: present
    `

how can I output the username of the third user only (the one that is 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
    with_items: ‘{{ linux_users }}’
    when: item.state != ‘absent’

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

{{ user.results[2].name }} outputs what I need, but obviously I won’t know which user will be created, so this needs to be dynamic. Is there some way to register an index integer of the user in the list that was created?

this is probably what you want:

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