Simple Loop

24 views
Skip to first unread message

Anand Solomon

unread,
Jan 27, 2020, 9:57:06 AM1/27/20
to Ansible Project
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


Dick Visser

unread,
Jan 27, 2020, 10:26:20 AM1/27/20
to ansible...@googlegroups.com
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?
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d8a7aad1-7272-44b7-b982-3673ec88190b%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Dick Visser

unread,
Jan 27, 2020, 10:34:25 AM1/27/20
to ansible...@googlegroups.com
On Mon, 27 Jan 2020 at 15:57, Anand Solomon <anand.v...@gmail.com> wrote:
>
> 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?



> 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
>
>

Anand Solomon

unread,
Jan 27, 2020, 10:53:16 AM1/27/20
to Ansible Project
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. 



On Monday, January 27, 2020 at 10:34:25 AM UTC-5, Dick Visser wrote:
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dick Visser

unread,
Jan 27, 2020, 11:03:57 AM1/27/20
to ansible...@googlegroups.com
If you supply the list as:

vars:
schemaname:
- user1
- user2
- 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 }}"
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a0d681bc-20c1-41f1-bc77-5e9903acf8b3%40googlegroups.com.

Anand Solomon

unread,
Jan 27, 2020, 6:39:20 PM1/27/20
to Ansible Project
This works now. Thanks.
Reply all
Reply to author
Forward
0 new messages