Skip to content

Instantly share code, notes, and snippets.

@jslatts
Created April 7, 2014 20:58
Show Gist options
  • Save jslatts/10053524 to your computer and use it in GitHub Desktop.
Save jslatts/10053524 to your computer and use it in GitHub Desktop.
SaltStack Custom Python State to Add Users
#!py
def run():
'''
Manage sudo enabled user adds/removes
'''
# temp fix for regression https://github.com/saltstack/salt/issues/7693
if '__pillar__' in globals():
globals()['pillar'] = globals()['__pillar__']
globals()['grains'] = globals()['__grains__']
globals()['salt'] = globals()['__salt__']
users_root = []
[users_root.extend(value) for key,value in pillar.iteritems() if key.startswith('users_add')]
generated_user_hsd = {}
for user in users_root:
generated_user_hsd[user] = {}
generated_user_hsd[user]['user'] = [
'present',
{'fullname' : user},
{'home' : '/home/' + user},
{'createHome': True},
{'shell' : '/bin/bash'},
{'optional_groups' : ['admin','ubuntu','wheel']}
]
if grains['os'] == 'Debian' or grains['os'] == 'Ubuntu':
generated_user_hsd[user]['file'] = [
'managed',
{'name' : '/etc/sudoers.d/' + user},
{'source' : 'salt://users/sudoers.ubuntu.jinja'},
{'template' : 'jinja'},
{'user' : 'root'},
{'group' : 'root'},
{'mode' : '0440'},
{'defaults' : {
'suser' : user,
'command' : 'ALL'
}
}
]
return generated_user_hsd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment