• Try the following code in your jobstep on both servers.

    if (select

    ars.role_desc

    from sys.dm_hadr_availability_replica_states ars

    inner join sys.availability_groups ag

    on ars.group_id = ag.group_id

    where ag.name = 'name of your AG group' -- you need to edit this line!!

    and ars.is_local = 1) = 'PRIMARY'

    begin

    insert your stored procedure here (EXEC database.schema.sp)-- you need to edit this line!! because SQL server parses your code this needs to be a stored procedure to avoid errors.

    end

    As database for the job, select master