We demonstrate precise control of the coupling of each of two trapped ions to the mode of an optical resonator. When both ions are coupled with near-maximum strength, we generate ion--ion entanglement heralded by the detection of two orthogonally polarized cavity photons. The entanglement fidelity with respect to the Bell state $Psi^+$ reaches $F geq (91.9pm2.5)%$. This result represents an important step toward distributed quantum computing with cavities linking remote atom-based registers.