# Outputting only changed list items

**URL:** <https://forum.ansible.com/t/outputting-only-changed-list-items/24748>\
**Category:** Ansible Project\
**Created:** [June 16, 2017, 7:13pm UTC](https://forum.ansible.com/t/outputting-only-changed-list-items/24748 "2017-06-16T19:13:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![B\_Holmes](https://avatars.discourse-cdn.com/v4/letter/b/a8b319/32.png) [@B\_Holmes](https://forum.ansible.com/u/B_Holmes)\
**Post date:** [June 16, 2017, 7:13pm UTC](https://forum.ansible.com/t/outputting-only-changed-list-items/24748/1 "2017-06-16T19:13:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 20, 2017, 3:09am UTC](https://forum.ansible.com/t/outputting-only-changed-list-items/24748/2 "2017-06-20T03:09:47Z")

</div>

this is probably what you want:

&nbsp;&nbsp;- name: Output username  
&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "User was created: {{ item.name }}"  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items: '{{ user.results }}'  
&nbsp;&nbsp;&nbsp;&nbsp;when: item|changed
