# How do I loop a task with variables defined in another file?

**URL:** <https://forum.ansible.com/t/how-do-i-loop-a-task-with-variables-defined-in-another-file/27236>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [April 11, 2018, 6:57am UTC](https://forum.ansible.com/t/how-do-i-loop-a-task-with-variables-defined-in-another-file/27236 "2018-04-11T06:57:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olof\_Lundgren](https://avatars.discourse-cdn.com/v4/letter/o/e19b73/32.png) [@Olof\_Lundgren](https://forum.ansible.com/u/Olof_Lundgren)\
**Post date:** [April 11, 2018, 6:57am UTC](https://forum.ansible.com/t/how-do-i-loop-a-task-with-variables-defined-in-another-file/27236/1 "2018-04-11T06:57:16Z")

</div>

I have built a .yml file with a set of users that I want to push to my routers with vyos\_user module.  
This is my **users.yml**

`  
users:

- full\_name: “Olof Lundgren”  
name: “olof”  
configured\_password: “abcd1234”  
level: “admin”  
ssh-key:  
name: “olof@olof-ubuntu-work”  
type: “ssh-rsa”  
key: “xxxxxxxxxxxxxxx”

- full\_name: “T7D VyOS default user”  
name: “vyos”  
configured\_password: “vyos”  
level: “admin”

- full\_name: “Servicedesk operator user”  
name: “servicedesk”  
configured\_password: “password”  
level: “operator”  
`

I built my playbook, but I dont know how to use this list of users in the play.

This is my attempt: **add-users-vyos.yml**

`

- hosts: vyos  
connection: network\_cli  
gather\_facts: no

tasks:

- name: Include user database  
include\_vars:  
file: users.yml

# name: vyosusers

- name: add and set users  
vyos\_user:  
full\_name: “{{ users.full\_name }}”  
configured\_password: “{{ users.configured\_password }}”  
name: “{{ users.name }}”  
level: admin  
purge: no  
state: present  
update\_password: always  
loop: “{{ users }}”  
`

First it successfully plays the task “Include user database” and I can see the variables are defined  
The on the next task. my error message is this:

`TASK [add and set users] ************************************************************************************************************************************************************************************ fatal: [vyos-router.lan]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'configured_password'\n\nThe error appears to have been in '/home/olof/Documents/ansible/add-users-vyos.yml': line 11, column 11, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: add and set users\n ^ here\n"}`

---

<div class="post-metadata">

**Author:** ![Olof\_Lundgren](https://avatars.discourse-cdn.com/v4/letter/o/e19b73/32.png) [@Olof\_Lundgren](https://forum.ansible.com/u/Olof_Lundgren)\
**Post date:** [April 11, 2018, 7:01am UTC](https://forum.ansible.com/t/how-do-i-loop-a-task-with-variables-defined-in-another-file/27236/2 "2018-04-11T07:01:23Z")

</div>

This is what the task “Include user database” debug message looks like when its _jsonprettyfied_

`

TASK [Include user database] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [vyos-router.lan] =\> {  
“ansible\_facts”: {  
“users”: [{  
“configured\_password”: “abcd1234”,  
“full\_name”: “Olof Lundgren”,  
“level”: “admin”,  
“name”: “olof”,  
“ssh-key”: {  
“key”: “xxxxxxxxxxxxxxxxx”,  
“name”: “olof@olof-ubuntu-work”,  
“type”: “ssh-rsa”  
}  
}, {  
“configured\_password”: “vyos”,  
“full\_name”: “T7D VyOS default user”,  
“level”: “admin”,  
“name”: “vyos”  
}, {  
“configured\_password”: “password”,  
“full\_name”: “Servicedesk operator user”,  
“level”: “operator”,  
“name”: “servicedesk”  
}]  
},  
“ansible\_included\_var\_files”: [“/home/olof/Documents/ansible/users.yml”],  
“changed”: false  
}  
`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 14, 2018, 9:01am UTC](https://forum.ansible.com/t/how-do-i-loop-a-task-with-variables-defined-in-another-file/27236/3 "2018-04-14T09:01:11Z")

</div>

When using loop/with\_ the current result is in a variable called item, so change all your {{ user.xxxx }} to {{ item.xxxx }}
