Re: [ansible-project] playbook to create multiple users?

8,435 views
Skip to first unread message

Brian Coca

unread,
May 22, 2013, 7:45:29 PM5/22/13
to ansible...@googlegroups.com
This should work (was missing quote):

  - name: create user(s)
    user: name=${item} shell=/bin/bash  
    with_file: "./user-list.txt"
    
    
adding same with_file here and item for file name should do the rest:

  - name: setup authorized key(s)
    action: authorized_key user=xmxb077 key='$FILE(/home/xmxb077/Code/Ansible/user_add/Users/${item}.pub)'
    with_file: "./user-list.txt"



--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Michael DeHaan

unread,
May 22, 2013, 10:33:15 PM5/22/13
to ansible...@googlegroups.com
I would totally simplify this rather than doing this as  you say.

Define your list of users in something like a "users.yml" file

In the play:

vars_files:
   - vars/users.yml

That defines a variable users, which is a list of strings.  Later this might be a hash of attributes about the user.

In the task:

with_items:  ${users}

Or in 1.2 syntax, simpler!

with_items: users

The idea of loading them from a text file is fine, but loading them from a YAML file so they are data is better.

You may also move on to wanting to keep those definitions in a group_vars/<groupname> file, so different hosts can get different lists of users, or even source that users list from external software later.






--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Michael Bogucki

unread,
May 23, 2013, 1:20:51 PM5/23/13
to ansible...@googlegroups.com
Hello Michael, Hello everyone,

First off thank you for the various suggestions. I ended up following Mr. DeHaan's suggestions. After a few tries, it finally "clicked" and I can see what the flow was like amongst the various files.
None-the-less, here's a posting of what I've done to help any new-comers (like me ^_^ )
As usual any suggestions are highly appreciated.

I'll probably setup a git-repository and post it there for future reference.


*** vars/users.yml  ***

project_root: /home/ubuntu/Code/Ansible/user_add
key_files: /home/ubuntu/Code/Ansible/user_add/Keys

users:
    - xmxb070
    - xlxr073
    - xtxb007
    - xmxk074
    - xaxu077

group_name:
    - users


*** user_add.yml ***

---

- hosts: localhost
  user: ubuntu
  vars_files:
    - vars/users.yml

  tasks:

      - name: create user keypair(s)
        command: /usr/bin/ssh-keygen -b 1024 -f ${item} -t rsa -N "" chdir=${key_files} creates=${key_files}/${item}
        with_items: ${users}


- hosts: test
  vars_files:
    - vars/users.yml
  sudo: yes

  tasks:

      - name: create user(s)
        user: name=${item} shell=/bin/bash group=${group_name}
        with_items: ${users}
   
      - name: Pushing authorized key(s) to remote server(s)
        action: authorized_key user=${item} key="$FILE(${key_files}/${item}.pub)"
        with_items: ${users}




Thanks again.

--Mike


Reply all
Reply to author
Forward
0 new messages