Writing SQL to check for a role if exist and add member
Your basic question boils down to "How can I tell if a given role exists in a database." All of the other code you already know how to do.

The easiest way is to query the sys.database_principals table to see if the role is there. Based on your sample question, here is a good check and update into the different roles:

IF EXISTS (
    SELECT * 
    FROM sys.database_principals 
    WHERE name = 'role_a' 
        and type = 'R'
        )
BEGIN   -- Role_A exists, add to role
    ALTER ROLE [role_a] ADD MEMBER [Domain/SqlAgent]
END
ELSE IF EXISTS (
    SELECT * 
    FROM sys.database_principals 
    WHERE name = 'role_b' 
        and type = 'R'
        )
BEGIN   -- Role_B exists, add to role
    ALTER ROLE [role_b] ADD MEMBER [Domain/SqlAgent]
END