# Loop using with\_items twice

**URL:** <https://forum.ansible.com/t/loop-using-with-items-twice/25039>\
**Category:** Ansible Project\
**Created:** [July 21, 2017, 1:49am UTC](https://forum.ansible.com/t/loop-using-with-items-twice/25039 "2017-07-21T01:49:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 21, 2017, 1:49am UTC](https://forum.ansible.com/t/loop-using-with-items-twice/25039/1 "2017-07-21T01:49:55Z")

</div>

What is the correct way to use with\_Items to loop over two files? The playbook is failing at the 2nd task

Getting an error - TASK [Create users from vars users] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘ansible.vars.unsafe\_proxy.AnsibleUnsafeText object’ has no attribute ‘users’\n\nThe error appears to have been in ‘/etc/ansible/playbooks/exercises/usersgroups.yml’: line 15, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Create users from vars users\n ^ here\n”}

users.yml (is below, grouplist.yml is similar)

---

<div class="post-metadata">

**Author:** ![azaghal](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/azaghal/32/4743_2.png) [@azaghal](https://forum.ansible.com/u/azaghal)\
**Post date:** [July 21, 2017, 12:32pm UTC](https://forum.ansible.com/t/loop-using-with-items-twice/25039/2 "2017-07-21T12:32:14Z")

</div>

Assuming that what you want to do is create a number of groups listed  
in the grouplist variable, and then create the users listed in the  
users variable, where each user is member of all groups in the  
grouplist variable, your second task should be one of the following  
(Ansible 2.3 can accept lists directly in "groups" attribute):

- name: Create users from vars users (Ansible 2.3+)  
&nbsp;&nbsp;user:  
&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;groups: "{{ grouplist }}"  
&nbsp;&nbsp;with\_items: "{{ userlist }}"

- name: Create users from vars users (Ansible \<2.3)  
&nbsp;&nbsp;user:  
&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;groups: "{{ grouplist | join(',') }}"  
&nbsp;&nbsp;with\_items: "{{ userlist }}"

Best regards

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 21, 2017, 1:26pm UTC](https://forum.ansible.com/t/loop-using-with-items-twice/25039/3 "2017-07-21T13:26:23Z")

</div>

Yes thats exactly what I wanted to do but I thought I had to use with\_items at the bottom to provide the list that it would loop through. Thanks
