#NEWLINE# def user_present( name , password = None , roles = None , encrypted = False , crypt_salt = None , algorithm = 'sha256' ) : #NEWLINE# #INDENT# '\n    Ensure a user is present with the specified groups\n\n    name\n        Name of user\n\n    password\n        Encrypted or Plain Text password for user\n\n    roles\n        List of roles the user should be assigned.  Any roles not in this list will be removed\n\n    encrypted\n        Whether the password is encrypted already or not.  Defaults to False\n\n    crypt_salt\n        Salt to use when encrypting the password.  Default is None (salt is\n        randomly generated for unhashed passwords)\n\n    algorithm\n        Algorithm to use for hashing password.  Defaults to sha256.\n        Accepts md5, blowfish, sha256, sha512\n\n        .. note: sha512 may make the hash too long to save in NX OS which limits the has to 64 characters\n\n    Examples:\n\n    .. code-block:: yaml\n\n        create:\n          nxos.user_present:\n            - name: daniel\n            - roles:\n              - vdc-admin\n\n        set_password:\n          nxos.user_present:\n            - name: daniel\n            - password: admin\n            - roles:\n              - network-admin\n\n        update:\n          nxos.user_present:\n            - name: daniel\n            - password: AiN9jaoP\n            - roles:\n              - network-admin\n              - vdc-admin\n\n    ' #NEWLINE# ret = { #NEWLINE# #INDENT# 'name' : name , #NEWLINE# 'result' : False , #NEWLINE# 'changes' : { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , #NEWLINE# 'comment' : '' , #NEWLINE# #UNINDENT# } #NEWLINE# change_password = False #NEWLINE# if ( password is not None ) : #NEWLINE# #INDENT# change_password = ( not __salt__ [ 'nxos.cmd' ] ( 'check_password' , username = name , password = password , encrypted = encrypted ) ) #NEWLINE# #UNINDENT# change_roles = False #NEWLINE# if ( roles is not None ) : #NEWLINE# #INDENT# cur_roles = __salt__ [ 'nxos.cmd' ] ( 'get_roles' , username = name ) #NEWLINE# change_roles = ( set ( roles ) != set ( cur_roles ) ) #NEWLINE# #UNINDENT# old_user = __salt__ [ 'nxos.cmd' ] ( 'get_user' , username = name ) #NEWLINE# if ( not any ( [ change_password , change_roles , ( not old_user ) ] ) ) : #NEWLINE# #INDENT# ret [ 'result' ] = True #NEWLINE# ret [ 'comment' ] = 'User already exists' #NEWLINE# return ret #NEWLINE# #UNINDENT# if ( change_roles is True ) : #NEWLINE# #INDENT# remove_roles = ( set ( cur_roles ) - set ( roles ) ) #NEWLINE# add_roles = ( set ( roles ) - set ( cur_roles ) ) #NEWLINE# #UNINDENT# if ( __opts__ [ 'test' ] is True ) : #NEWLINE# #INDENT# ret [ 'result' ] = None #NEWLINE# if ( not old_user ) : #NEWLINE# #INDENT# ret [ 'comment' ] = 'User will be created' #NEWLINE# if ( password is not None ) : #NEWLINE# #INDENT# ret [ 'changes' ] [ 'password' ] = True #NEWLINE# #UNINDENT# if ( roles is not None ) : #NEWLINE# #INDENT# ret [ 'changes' ] [ 'role' ] = { #NEWLINE# #INDENT# 'add' : roles , #NEWLINE# 'remove' : [ ] , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# return ret #NEWLINE# #UNINDENT# if ( change_password is True ) : #NEWLINE# #INDENT# ret [ 'comment' ] = 'User will be updated' #NEWLINE# ret [ 'changes' ] [ 'password' ] = True #NEWLINE# #UNINDENT# if ( change_roles is True ) : #NEWLINE# #INDENT# ret [ 'comment' ] = 'User will be updated' #NEWLINE# ret [ 'changes' ] [ 'roles' ] = { #NEWLINE# #INDENT# 'add' : list ( add_roles ) , #NEWLINE# 'remove' : list ( remove_roles ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# return ret #NEWLINE# #UNINDENT# if ( change_password is True ) : #NEWLINE# #INDENT# new_user = __salt__ [ 'nxos.cmd' ] ( 'set_password' , username = name , password = password , encrypted = encrypted , role = ( roles [ 0 ] if roles else None ) , crypt_salt = crypt_salt , algorithm = algorithm ) #NEWLINE# ret [ 'changes' ] [ 'password' ] = { #NEWLINE# #INDENT# 'new' : new_user , #NEWLINE# 'old' : old_user , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( change_roles is True ) : #NEWLINE# #INDENT# for role in add_roles : #NEWLINE# #INDENT# __salt__ [ 'nxos.cmd' ] ( 'set_role' , username = name , role = role ) #NEWLINE# #UNINDENT# for role in remove_roles : #NEWLINE# #INDENT# __salt__ [ 'nxos.cmd' ] ( 'unset_role' , username = name , role = role ) #NEWLINE# #UNINDENT# ret [ 'changes' ] [ 'roles' ] = { #NEWLINE# #INDENT# 'new' : __salt__ [ 'nxos.cmd' ] ( 'get_roles' , username = name ) , #NEWLINE# 'old' : cur_roles , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# correct_password = True #NEWLINE# if ( password is not None ) : #NEWLINE# #INDENT# correct_password = __salt__ [ 'nxos.cmd' ] ( 'check_password' , username = name , password = password , encrypted = encrypted ) #NEWLINE# #UNINDENT# correct_roles = True #NEWLINE# if ( roles is not None ) : #NEWLINE# #INDENT# cur_roles = __salt__ [ 'nxos.cmd' ] ( 'get_roles' , username = name ) #NEWLINE# correct_roles = ( set ( roles ) != set ( cur_roles ) ) #NEWLINE# #UNINDENT# if ( not correct_password ) : #NEWLINE# #INDENT# ret [ 'comment' ] = 'Failed to set correct roles' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not correct_password ) : #NEWLINE# #INDENT# ret [ 'comment' ] = 'Failed to set correct password' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ret [ 'comment' ] = 'User set correctly' #NEWLINE# ret [ 'result' ] = True #NEWLINE# #UNINDENT# #UNINDENT# return ret