Reputation: 21
I have a yaml for the creation of a user.
users:
username:
uid: 12345
gid: 6789
secggroups:
- group1
- group3
gecos: user_for_xyz
home: /home/username
I also have a file with just the usernames
called users_list
. The playbook to create users is as follows:
---
\- name: create users
user: name="{{ item }}" uid={{ users[item]['uid'] }} group={{ users[item][gid] }} comment="{{ users[item]['gecos'] }}" home={{ users[item]['home' }} expires=0
with_items:
\- users_list
How can I loop through the groups to be added to user?
Upvotes: 2
Views: 9183
Reputation: 1144
You can solve this using subelements like this:
- name: User creation
user:
name:"{{item.0.username}}"
groups: "{{item.1 }}"
with_subelements:
- "{{ users }}"
- groups
here is a sample to debug, like a directory tree:
vars:
test:
- name: Testing subelements loop
dir: dir0
subdir:
- subdir0
- subdir1
- subdir2
tasks:
- name: Subelements loop sample
debug:
msg: "{{ item.0.dir }}/{{ item.1 }}"
with_subelements:
- "{{ test }}"
- subdir
you can find more here: http://docs.ansible.com/ansible/playbooks_loops.html#standard-loops
Upvotes: 0
Reputation: 34426
Your playbook is on the right track.
Try this for the users variable:
users:
- username: someusername
uid: 12345
gid: 6789
groups:
- group1
- group3
gecos: "Some user"
home: /home/someusername
- username: someusername
... etc ...
And this for the user play
- name: User creation
user:
name:"{{item.username}}"
groups: "{{item.groups | join(',')}}"
comment: "{{item.name}}"
uid: "{{item.uid}}"
with_items: users
Note that I modified your syntax to not use inline YAML.
Also you may find this users role helpful.
Upvotes: 0