mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 01:18:58 +00:00
pdbedit.list - list accounts in passdb
This commit is contained in:
parent
bd10ff57bf
commit
bbf0cd082a
87
salt/modules/pdbedit.py
Normal file
87
salt/modules/pdbedit.py
Normal file
@ -0,0 +1,87 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Module for Samba's pdbedit tool
|
||||
'''
|
||||
from __future__ import absolute_import
|
||||
|
||||
# Import Python libs
|
||||
import logging
|
||||
|
||||
# Import Salt libs
|
||||
import salt.utils
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Define the module's virtual name
|
||||
__virtualname__ = 'pdbedit'
|
||||
|
||||
# Function aliases
|
||||
__func_alias__ = {
|
||||
'list_users': 'list'
|
||||
}
|
||||
|
||||
|
||||
def __virtual__():
|
||||
'''
|
||||
Provides pdbedit if available
|
||||
'''
|
||||
if salt.utils.which('pdbedit'):
|
||||
return __virtualname__
|
||||
return (
|
||||
False,
|
||||
'{0} module can only be loaded when pdbedit is available'.format(
|
||||
__virtualname__
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def list_users(verbose=True):
|
||||
'''
|
||||
List user accounts
|
||||
|
||||
verbose : boolean
|
||||
return all information
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' pdbedit.list
|
||||
'''
|
||||
users = {} if verbose else []
|
||||
|
||||
if verbose:
|
||||
## parse detailed user data
|
||||
res = __salt__['cmd.run_all']('pdbedit -L -v')
|
||||
|
||||
if res['retcode'] > 0:
|
||||
log.error(res['stderr'] if 'stderr' in res else res['stdout'])
|
||||
else:
|
||||
user_data = {}
|
||||
for user in res['stdout'].splitlines():
|
||||
if user.startswith('-'):
|
||||
if len(user_data) > 0:
|
||||
users[user_data['unix username']] = user_data
|
||||
user_data = {}
|
||||
else:
|
||||
label = user[:user.index(':')].strip().lower()
|
||||
data = user[(user.index(':')+1):].strip()
|
||||
if len(data) > 0:
|
||||
user_data[label] = data
|
||||
|
||||
if len(user_data) > 0:
|
||||
users[user_data['unix username']] = user_data
|
||||
else:
|
||||
## list users
|
||||
res = __salt__['cmd.run_all']('pdbedit -L')
|
||||
|
||||
if res['retcode'] > 0:
|
||||
log.error(res['stderr'] if 'stderr' in res else res['stdout'])
|
||||
else:
|
||||
for user in res['stdout'].splitlines():
|
||||
users.append(user.split(':')[0])
|
||||
|
||||
return users
|
||||
|
||||
|
||||
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
|
Loading…
Reference in New Issue
Block a user