1. Get all custom roles from the portal, I presumed that the portal already has custom roles other than (org_user, org_publisher, org_admin)
from arcgis.gis import *
gis= GIS(portalUrl, userName, password)
allRoles = gis.users.roles.all(max_roles=50)
2. Create a User with Default Role
username="test"
password=''
firstname='test_name'
lastname='test_last'
email='test@test.com'
description = 'Test Account'
role = 'org_publisher'
provider = 'enterprise'
idp_username = 'test'
level = 2
thumbnail = None
newUser = gis.users.create(username, password,firstname,
lastname, email, description, role, provider,
idp_username, level, thumbnail)
3. Update the defualt role to Custom Role - Assign the role object not role name
#Assigning the first role out of many portal roles, as an example
status = newUser.update_role(role=allRoles[0])
print(status)
If role assigned success, the value of status will be True.
Reference:
https://developers.arcgis.com/python/guide/accessing-and-managing-users/