# Simple Loop

**URL:** <https://forum.ansible.com/t/simple-loop/32066>\
**Category:** Ansible Project\
**Created:** [January 27, 2020, 2:57pm UTC](https://forum.ansible.com/t/simple-loop/32066 "2020-01-27T14:57:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Solomon](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@Anand\_Solomon](https://forum.ansible.com/u/Anand_Solomon)\
**Post date:** [January 27, 2020, 2:57pm UTC](https://forum.ansible.com/t/simple-loop/32066/1 "2020-01-27T14:57:06Z")

</div>

Hi,  
I want to create users in the database using Ansible. I need ansible to run the task (creating user) for all the users mentioned in the vars: schemaname. How do I do that in loop ?

var:  
schemaname: user1, user2, user3

tasks:

- name: This playbook will create the user.  
oracle\_user:  
oracle\_home: /home/ansible/instantclient\_12\_2  
user: “{{user}}”  
password: “{{password}}”  
service\_name: “{{sname}}”  
port: “{{prt}}”  
hostname: “{{hostname}}”  
schema: “{{schemaname}}”  
schema\_password:  
default\_tablespace: USERS  
state: present  
update\_password: on\_create  
grants: “DBA”  
environment: “{{oracle\_env}}”  
register: user\_create  
delegate\_to: localhost

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 27, 2020, 3:25pm UTC](https://forum.ansible.com/t/simple-loop/32066/2 "2020-01-27T15:25:52Z")

</div>

You'd have to manually split that and trim the results so you can use  
it in a loop.  
Where does that "user1, user2, user3" string come from?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 27, 2020, 3:33pm UTC](https://forum.ansible.com/t/simple-loop/32066/3 "2020-01-27T15:33:54Z")

</div>

> Hi,  
> I want to create users in the database using Ansible. I need ansible to run the task (creating user) for all the users mentioned in the vars: schemaname. How do I do that in loop ?

That list of users only has the usernames.  
If you loop over them, and that is all the information you have, they  
will all have the same password.  
Are you sure that is what you want?

---

<div class="post-metadata">

**Author:** ![Anand\_Solomon](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@Anand\_Solomon](https://forum.ansible.com/u/Anand_Solomon)\
**Post date:** [January 27, 2020, 3:53pm UTC](https://forum.ansible.com/t/simple-loop/32066/4 "2020-01-27T15:53:16Z")

</div>

Yes, All I want to create is the user from the list (vars). This will be hard coded value from the vars and the password will be the same for all the users.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 27, 2020, 4:03pm UTC](https://forum.ansible.com/t/simple-loop/32066/5 "2020-01-27T16:03:24Z")

</div>

If you supply the list as:

vars:  
&nbsp;&nbsp;schemaname:  
&nbsp;&nbsp;&nbsp;&nbsp;- user1  
&nbsp;&nbsp;&nbsp;&nbsp;- user2  
&nbsp;&nbsp;&nbsp;&nbsp;- user3

then it's as easy as:

loop: "{{ schemaname }}"

If you get that comma delimited string from somewhere else (which I  
asked but got no response to), then you'd have to manually split and  
trim:

loop: "{{ schemaname.split(',') | map('trim') | list }}"

---

<div class="post-metadata">

**Author:** ![Anand\_Solomon](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@Anand\_Solomon](https://forum.ansible.com/u/Anand_Solomon)\
**Post date:** [January 27, 2020, 11:39pm UTC](https://forum.ansible.com/t/simple-loop/32066/6 "2020-01-27T23:39:20Z")

</div>

This works now. Thanks.
