playbook to create multiple users?

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”

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/ file, so different hosts can get different lists of users, or even source that users list from external software later.

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 :slight_smile: )
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 ***