# Looping and Error handling

**URL:** <https://forum.ansible.com/t/looping-and-error-handling/21914>\
**Category:** Ansible Project\
**Created:** [July 4, 2016, 6:40am UTC](https://forum.ansible.com/t/looping-and-error-handling/21914 "2016-07-04T06:40:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![megha\_sharma](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@megha\_sharma](https://forum.ansible.com/u/megha_sharma)\
**Post date:** [July 4, 2016, 6:40am UTC](https://forum.ansible.com/t/looping-and-error-handling/21914/1 "2016-07-04T06:40:33Z")

</div>

I am trying to add multiple users on my machine using Looping concept in ansible. Along with that I want to handle the errors occur when i try to add a user that already exists.But I am not able to rescue each iteration.

**This is my code** :  
#Users that I am trying to add already exists

- name: Add a user  
hosts: server  
gather\_facts: false  
tasks:
- name: Create Group  
win\_group:  
name: TestGroup  
state: present
- block:
- name: Adding Multiple Users  
win\_user: name={{ item.name }} password={{ item.password }} state=present groups={{ item.groups }}  
register: AddUser  
with\_items:
- { name: ‘testuser1’, password: ‘test1@123’, groups: ‘TestGroup’ }
- { name: ‘testuser2’, password: ‘test2@123’, groups: ‘TestGroup’ }  
rescue:
- debug: msg=‘Command failed…please check…may be User Already exists’  
when: “‘FAILED’ in AddUser”

**Output :**

PLAY [Add a user] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Create Group] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [10.155.101.39]

TASK [Adding Multiple Users] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [10.155.101.39] =\> (item={u’password’: u’infy@123’, u’name’: u’testuser1’, u’groups’: u’TestGroup’}) =\> {“failed”: true, “item”: {“groups”: “TestGroup”, “name”: “testuser1”, “password”: “infy@123”}, “msg”: “Exception calling "ValidateCredentials" with "2" argument(s): "The network path was not found.\r\n"”}  
failed: [10.155.101.39] =\> (item={u’password’: u’infy@123’, u’name’: u’testuser2’, u’groups’: u’TestGroup’}) =\> {“failed”: true, “item”: {“groups”: “TestGroup”, “name”: “testuser2”, “password”: “infy@123”}, “msg”: “Exception calling "ValidateCredentials" with "2" argument(s): "The network path was not found.\r\n"”}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [10.155.101.39] =\> {  
“msg”: “Command failed…please check…may be User Already exists”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
10.155.101.39 : ok=2 changed=0 unreachable=0 failed=0
