Lopetegui signed a deal until 2020 on May 22 but Real Madrid have swooped for his services and he has agreed to join the Spanish giants after the World Cup.
The 51-year-old represented Barcelona as a player for three years but will now take charge of their bitter rivals.
The move comes as a surprise, with Lopetegui not featuring prominently among the host of managers linked with the role since the departure of Zinedine Zidane days after Madrid’s 3-1 victory over Liverpool in the Champions League final.
Arsene Wenger, Antonio Conte and Guti were among the front runners to replace Zidane but Lopetegui has pipped the trio to one of the most coveted jobs in world football.
Lopetegui, who has been in charge of the national team for the past two years, has agreed a three-year contract at the Bernabeu.