Outputting only changed list items

13 views
Skip to first unread message

B Holmes

unread,
Jun 19, 2017, 11:35:29 AM6/19/17
to Ansible Project
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?



Brian Coca

unread,
Jun 19, 2017, 11:09:55 PM6/19/17
to Ansible Project
this is probably what you want:

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

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages