Lopetegui will put to pen to paper on a three-year deal at the Bernabéu running until 2021, the club confirmed.
A former goalkeeper, Lopetegui made one senior appearance for Real Madrid but played 107 matches for Logrones.
Lopetegui, 51, has been in charge of the Spanish national team since July 2016, when he took the reins following the European Championship in France.
He began coaching in 2003 as assistant manager to Spain's Under-17 team and was also in charge of the Under-19s, Under-20s and Under-21s, before moving to Porto.
Lopetegui is now preparing to lead Spain at the World Cup but the 51-year-old will depart his role as national team coach at the end of the tournament.
After guiding Spain easily through qualification to the World Cup finals, Lopetegui had agreed on a new contract up until Euro 2022 just last May, and was not among the more than 20 names which had been linked with the Bernabeu vacancy in the local media over the past two weeks. Spain will face Iran and Morocco in Group B after the challenge of Cristiano Ronaldo's Portugal in their opener on Friday.