Merge pull request #2822 from adfinis-forks/fix_udm_user

univention udm_user: pep8
This commit is contained in:
René Moser 2016-08-31 14:29:34 +02:00 committed by GitHub
commit 48eba12d23

View file

@ -21,6 +21,8 @@
# #
from datetime import date
import crypt
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.univention_umc import ( from ansible.module_utils.univention_umc import (
umc_module_for_add, umc_module_for_add,
@ -28,9 +30,7 @@ from ansible.module_utils.univention_umc import (
ldap_search, ldap_search,
base_dn, base_dn,
) )
from datetime import date
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
import crypt
DOCUMENTATION = ''' DOCUMENTATION = '''
@ -40,7 +40,8 @@ version_added: "2.2"
author: "Tobias Rueetschi (@2-B)" author: "Tobias Rueetschi (@2-B)"
short_description: Manage posix users on a univention corporate server short_description: Manage posix users on a univention corporate server
description: description:
- "This module allows to manage posix users on a univention corporate server (UCS). - "This module allows to manage posix users on a univention corporate
server (UCS).
It uses the python API of the UCS to create a new object or edit it." It uses the python API of the UCS to create a new object or edit it."
requirements: requirements:
- Python >= 2.6 - Python >= 2.6
@ -84,36 +85,40 @@ options:
default: None default: None
description: description:
- Country of users business address. - Country of users business address.
departmentNumber: department_number:
required: false required: false
default: None default: None
description: description:
- Department number of users business address. - Department number of users business address.
aliases: [ departmentNumber ]
description: description:
required: false required: false
default: None default: None
description: description:
- Description (not gecos) - Description (not gecos)
displayName: display_name:
required: false required: false
default: None default: None
description: description:
- Display name (not gecos) - Display name (not gecos)
aliases: [ displayName ]
email: email:
required: false required: false
default: [''] default: ['']
description: description:
- A list of e-mail addresses. - A list of e-mail addresses.
employeeNumber: employee_number:
required: false required: false
default: None default: None
description: description:
- Employee number - Employee number
employeeType: aliases: [ employeeNumber ]
employee_type:
required: false required: false
default: None default: None
description: description:
- Employee type - Employee type
aliases: [ employeeType ]
gecos: gecos:
required: false required: false
default: None default: None
@ -126,57 +131,65 @@ options:
- "POSIX groups, the LDAP DNs of the groups will be found with the - "POSIX groups, the LDAP DNs of the groups will be found with the
LDAP filter for each group as $GROUP: LDAP filter for each group as $GROUP:
C((&(objectClass=posixGroup)(cn=$GROUP)))." C((&(objectClass=posixGroup)(cn=$GROUP)))."
homeShare: home_share:
required: false required: false
default: None default: None
description: description:
- "Home NFS share. Must be a LDAP DN, e.g. - "Home NFS share. Must be a LDAP DN, e.g.
C(cn=home,cn=shares,ou=school,dc=example,dc=com)." C(cn=home,cn=shares,ou=school,dc=example,dc=com)."
homeSharePath: aliases: [ homeShare ]
home_share_path:
required: false required: false
default: None default: None
description: description:
- Path to home NFS share, inside the homeShare. - Path to home NFS share, inside the homeShare.
homeTelephoneNumber: aliases: [ homeSharePath ]
home_telephone_number:
required: false required: false
default: [] default: []
description: description:
- List of private telephone numbers. - List of private telephone numbers.
aliases: [ homeTelephoneNumber ]
homedrive: homedrive:
required: false required: false
default: None default: None
description: description:
- Windows home drive, e.g. C("H:"). - Windows home drive, e.g. C("H:").
mailAlternativeAddress: mail_alternative_address:
required: false required: false
default: [] default: []
description: description:
- List of alternative e-mail addresses. - List of alternative e-mail addresses.
mailHomeServer: aliases: [ mailAlternativeAddress ]
mail_home_server:
required: false required: false
default: None default: None
description: description:
- FQDN of mail server - FQDN of mail server
mailPrimaryAddress: aliases: [ mailHomeServer ]
mail_primary_address:
required: false required: false
default: None default: None
description: description:
- Primary e-mail address - Primary e-mail address
mobileTelephoneNumber: aliases: [ mailPrimaryAddress ]
mobile_telephone_number:
required: false required: false
default: [] default: []
description: description:
- Mobile phone number - Mobile phone number
aliases: [ mobileTelephoneNumber ]
organisation: organisation:
required: false required: false
default: None default: None
description: description:
- Organisation - Organisation
pagerTelephonenumber: pager_telephonenumber:
required: false required: false
default: [] default: []
description: description:
- List of pager telephone numbers. - List of pager telephone numbers.
aliases: [ pagerTelephonenumber ]
phone: phone:
required: false required: false
default: [] default: []
@ -187,38 +200,43 @@ options:
default: None default: None
description: description:
- Postal code of users business address. - Postal code of users business address.
primaryGroup: primary_group:
required: false required: false
default: cn=Domain Users,cn=groups,$LDAP_BASE_DN default: cn=Domain Users,cn=groups,$LDAP_BASE_DN
description: description:
- Primary group. This must be the group LDAP DN. - Primary group. This must be the group LDAP DN.
aliases: [ primaryGroup ]
profilepath: profilepath:
required: false required: false
default: None default: None
description: description:
- Windows profile directory - Windows profile directory
pwdChangeNextLogin: pwd_change_next_login:
required: false required: false
default: None default: None
choices: [ '0', '1' ] choices: [ '0', '1' ]
description: description:
- Change password on next login. - Change password on next login.
roomNumber: aliases: [ pwdChangeNextLogin ]
room_number:
required: false required: false
default: None default: None
description: description:
- Room number of users business address. - Room number of users business address.
sambaPrivileges: aliases: [ roomNumber ]
samba_privileges:
required: false required: false
default: [] default: []
description: description:
- "Samba privilege, like allow printer administration, do domain - "Samba privilege, like allow printer administration, do domain
join." join."
sambaUserWorkstations: aliases: [ sambaPrivileges ]
samba_user_workstations:
required: false required: false
default: [] default: []
description: description:
- Allow the authentication only on this Microsoft Windows host. - Allow the authentication only on this Microsoft Windows host.
aliases: [ sambaUserWorkstations ]
sambahome: sambahome:
required: false required: false
default: None default: None
@ -268,8 +286,8 @@ options:
required: false required: false
default: '' default: ''
description: description:
- "Define the whole position of users object inside the LDAP tree, e.g. - "Define the whole position of users object inside the LDAP tree,
C(cn=employee,cn=users,ou=school,dc=example,dc=com)." e.g. C(cn=employee,cn=users,ou=school,dc=example,dc=com)."
ou: ou:
required: false required: false
default: '' default: ''
@ -314,7 +332,7 @@ RETURN = '''# '''
def main(): def main():
expiry = date.strftime(date.today()+relativedelta(years=1), "%Y-%m-%d") expiry = date.strftime(date.today() + relativedelta(years=1), "%Y-%m-%d")
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
birthday = dict(default=None, birthday = dict(default=None,
@ -323,46 +341,58 @@ def main():
type='str'), type='str'),
country = dict(default=None, country = dict(default=None,
type='str'), type='str'),
departmentNumber = dict(default=None, department_number = dict(default=None,
type='str'), type='str',
aliases=['departmentNumber']),
description = dict(default=None, description = dict(default=None,
type='str'), type='str'),
displayName = dict(default=None, display_name = dict(default=None,
type='str'), type='str',
aliases=['displayName']),
email = dict(default=[''], email = dict(default=[''],
type='list'), type='list'),
employeeNumber = dict(default=None, employee_number = dict(default=None,
type='str'), type='str',
employeeType = dict(default=None, aliases=['employeeNumber']),
type='str'), employee_type = dict(default=None,
type='str',
aliases=['employeeType']),
firstname = dict(default=None, firstname = dict(default=None,
type='str'), type='str'),
gecos = dict(default=None, gecos = dict(default=None,
type='str'), type='str'),
groups = dict(default=[], groups = dict(default=[],
type='list'), type='list'),
homeShare = dict(default=None, home_share = dict(default=None,
type='str'), type='str',
homeSharePath = dict(default=None, aliases=['homeShare']),
type='str'), home_share_path = dict(default=None,
homeTelephoneNumber = dict(default=[], type='str',
type='list'), aliases=['homeSharePath']),
home_telephone_number = dict(default=[],
type='list',
aliases=['homeTelephoneNumber']),
homedrive = dict(default=None, homedrive = dict(default=None,
type='str'), type='str'),
lastname = dict(default=None, lastname = dict(default=None,
type='str'), type='str'),
mailAlternativeAddress = dict(default=[], mail_alternative_address= dict(default=[],
type='list'), type='list',
mailHomeServer = dict(default=None, aliases=['mailAlternativeAddress']),
type='str'), mail_home_server = dict(default=None,
mailPrimaryAddress = dict(default=None, type='str',
type='str'), aliases=['mailHomeServer']),
mobileTelephoneNumber = dict(default=[], mail_primary_address = dict(default=None,
type='list'), type='str',
aliases=['mailPrimaryAddress']),
mobile_telephone_number = dict(default=[],
type='list',
aliases=['mobileTelephoneNumber']),
organisation = dict(default=None, organisation = dict(default=None,
type='str'), type='str'),
pagerTelephonenumber = dict(default=[], pager_telephonenumber = dict(default=[],
type='list'), type='list',
aliases=['pagerTelephonenumber']),
password = dict(default=None, password = dict(default=None,
type='str', type='str',
no_log=True), no_log=True),
@ -370,19 +400,24 @@ def main():
type='list'), type='list'),
postcode = dict(default=None, postcode = dict(default=None,
type='str'), type='str'),
primaryGroup = dict(default=None, primary_group = dict(default=None,
type='str'), type='str',
aliases=['primaryGroup']),
profilepath = dict(default=None, profilepath = dict(default=None,
type='str'), type='str'),
pwdChangeNextLogin = dict(default=None, pwd_change_next_login = dict(default=None,
type='str', type='str',
choices=['0', '1']), choices=['0', '1'],
roomNumber = dict(default=None, aliases=['pwdChangeNextLogin']),
type='str'), room_number = dict(default=None,
sambaPrivileges = dict(default=[], type='str',
type='list'), aliases=['roomNumber']),
sambaUserWorkstations = dict(default=[], samba_privileges = dict(default=[],
type='list'), type='list',
aliases=['sambaPrivileges']),
samba_user_workstations = dict(default=[],
type='list',
aliases=['sambaUserWorkstations']),
sambahome = dict(default=None, sambahome = dict(default=None,
type='str'), type='str'),
scriptpath = dict(default=None, scriptpath = dict(default=None,
@ -449,25 +484,25 @@ def main():
else: else:
obj = umc_module_for_edit('users/user', user_dn) obj = umc_module_for_edit('users/user', user_dn)
if module.params['displayName'] == None: if module.params['displayName'] is None:
module.params['displayName'] = '{} {}'.format( module.params['displayName'] = '{} {}'.format(
module.params['firstname'], module.params['firstname'],
module.params['lastname'] module.params['lastname']
) )
if module.params['unixhome'] == None: if module.params['unixhome'] is None:
module.params['unixhome'] = '/home/{}'.format( module.params['unixhome'] = '/home/{}'.format(
module.params['username'] module.params['username']
) )
for k in obj.keys(): for k in obj.keys():
if (k != 'password' and if (k != 'password' and
k != 'groups' and k != 'groups' and
module.params.has_key(k) and k in module.params and
module.params[k] != None): module.params[k] is not None):
obj[k] = module.params[k] obj[k] = module.params[k]
# handle some special values # handle some special values
obj['e-mail'] = module.params['email'] obj['e-mail'] = module.params['email']
password = module.params['password'] password = module.params['password']
if obj['password'] == None: if obj['password'] is None:
obj['password'] = password obj['password'] = password
else: else:
old_password = obj['password'].split('}', 2)[1] old_password = obj['password'].split('}', 2)[1]
@ -488,12 +523,17 @@ def main():
obj.modify() obj.modify()
except: except:
module.fail_json( module.fail_json(
msg="Creating/editing user {} in {} failed".format(username, container) msg="Creating/editing user {} in {} failed".format(
username,
container
)
) )
try: try:
groups = module.params['groups'] groups = module.params['groups']
if groups: if groups:
filter = '(&(objectClass=posixGroup)(|(cn={})))'.format(')(cn='.join(groups)) filter = '(&(objectClass=posixGroup)(|(cn={})))'.format(
')(cn='.join(groups)
)
group_dns = list(ldap_search(filter, attr=['dn'])) group_dns = list(ldap_search(filter, attr=['dn']))
for dn in group_dns: for dn in group_dns:
grp = umc_module_for_edit('groups/group', dn[0]) grp = umc_module_for_edit('groups/group', dn[0])