Cavity control of rare earth ions