He was born to peasants in Valle de Santiago, Guanajuato, and ran away from home as a child to see other lands and customs of the people.
In November 1853 he commanded the forces that defeated William Walker and filibusters who tried to seize Baja California and Sonora to create an independent republic.
He was designated governor of Sonora on April 19, 1854, and, on July 13 of the same year, he defended the state against an army under the command of Count Gaston de Raousset-Boulbon.
Count Boulbon had tried to convince Yáñez to give up Guaymas without resistance and, after his refusal, attacked the port on July 13.
Neighboring Mexican states joined the army commanded by Yañez and Boulbon was arrested and executed on August 13.