There's a few ways to automate this, but I'd start with something simple and use Azure Automation.
Azure Automation allows you to automate repetitive tasks and workflows using PowerShell runbooks or Python scripts.
You can create runbooks that define the custom roles, their permissions, and assign them to resource groups or specific resources.
Azure Automation integrates with Azure Resource Manager, allowing you to manage resources and configurations at scale.