Commit d75a912f authored by Scott Moser's avatar Scott Moser

fix adding of users when no group is specified

revision 1179 regressed adding a user that did not have a 'groups'
entry present in cloud-config.
This handles that correctly, making 'add_user' able to take:
  a.) groups="group1,group2"
  b.) groups=["group1", "group2"]
  c.) groups=None
  d.) no groups parameter

Additionally, if a primary group is specified it will also be created.

End result is that this is functional:
 groups: ["sudo"]
   - name: sysop
     primary-group: sysop
     groups: "sudo,adm"
     shell: /bin/bash
   - name: user1
     primary-group: users
     groups: sudo
   - name: foo1
   - name: bar
     gecos: Bar
     groups: ["bargroup"]

Resulting in:
 $ groups sysop
 sysop : sysop adm sudo
 $ groups user1
 user1 : users sudo
 $ groups foo1
 foo1 : foo1
 $ groups bar
 bar : bar bargroup

LP: #1562918
parents 6a660b49 2c95e4cf
......@@ -362,15 +362,22 @@ class Distro(object):
redact_opts = ['passwd']
# support kwargs having groups=[list] or groups="g1,g2"
groups = kwargs.get('groups')
if groups:
if isinstance(groups, (list, tuple)):
# kwargs.items loop below wants a comma delimeted string
# that can go right through to the command.
kwargs['groups'] = ",".join(groups)
groups = groups.split(",")
if create_groups:
for group in kwargs.get('groups').split(","):
primary_group = kwargs.get('primary_group')
if primary_group:
if create_groups and groups:
for group in groups:
if not util.is_group(group):
LOG.debug("created group %s for user %s", name, group)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment