Thoughts on role for creating multiple users

40 views
Skip to first unread message

appeltabak

unread,
Sep 1, 2016, 11:55:00 AM9/1/16
to Ansible Project
Hi,

Currently, I'm transforming my user role from creating one user to creating multiple users. In many examples I've seen this is done by adding with_items to every tasks related to the creation of the user (create user, add authorized key, etc). This would result in executing every tasks for every listed user before moving to the next tasks (right?) But what I want is that the whole task file is run from top to bottom, for every listed user.

I've tried this approach: https://gist.github.com/Appeltabak/7f20f9f6e4fed80202cc2dd5d0044b5f
Unfortunately, this gives an undefined variable error when the outer_users is used in subsequent tasks.

TL;DR How can I run a task file with
with_items ?

Thanks

Iain M Conochie

unread,
Sep 2, 2016, 5:58:59 AM9/2/16
to ansible...@googlegroups.com
OK - so you define your user vars like this in vars/users.yml:

users:
  - name: User of apples
    user: apple
    uid: 500
    home: /home/apple
    shell: /bin/bash
  - name: Orange Guy
    user: orange
    uid: 501
    home: /home/orange
    shell: /bin/bash
                       
Then a basic playbook would be:

---

- name: Users
  become: true
  hosts: hostlist
  vars_files:
    - vars/users.yml

  tasks:
  - name: Create new users
    user:
      createhome: yes
      home: "{{ item.home }}"
      comment: "{{ item.name }}"
      user: "{{ item.user }}"
      uid: "{{ item.uid }}"
      shell: "{{ item.shell }}"
    with_items: "{{ users }}"

Cheers

Iain


Thanks
--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5c62db30-0ef4-4732-a545-726a501ca53f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages