With only two days to go before Spain's opening match at the World Cup, Julen Lopetegui was fired as national team coach after accepting a job to lead Real Madrid next season.

Spanish soccer federation president Luis Rubiales, who made the announcement on Wednesday in Krasnodar, later said Fernando Hierro would replace Lopetegui as coach for Spain's match against Portugal in Sochi on Friday.

The 50-year-old Hierro, a former national team player and Real Madrid captain, will be taking on his first major coaching job. He had been acting as the federation's sports director and was already in Russia with the national team.

"I couldn't have said 'no,' I wouldn't forgive myself," Hierro said. "We still have a great opportunity in front of us. We have to remain positive and move forward."

Rubiales said firing Lopetegui wasn't the best solution but it was needed after the federation was caught by surprise by Madrid's announcement .

"The federation cannot be left out of a negotiation by one of its workers and be informed five minutes before the press release," Rubiales said. "We have been compelled to act."

Rubiales, who took over as president last month, said Lopetegui disrespected the federation's values and it was the only decision he could make.

"It's a difficult situation, but we are not the ones who determined the action that had to be taken. The federation has its values and it has to maintain them," Rubiales said. "It may look like a weakness now, but with time this will make us stronger."

Rubiales said he tried to keep the Madrid announcement from being made public but it wasn't possible. He said he had to make the decision — which was supported by the federation's board of directors — to "send a clear message" that things have to be done properly.

There were reports that players tried to persuade Rubiales to change his mind and keep Lopetegui on the job.

"We are the national team. We represent this emblem, these colors, our fans, a country," Spain captain Sergio Ramos wrote on Twitter. "Our responsibility and commitment is to you. Yesterday, today and tomorrow, together."

Because he was fired, Madrid won't have to pay the 2 million euros ($2.3 million) of the termination clause on Lopetegui's contract. Rubiales said that although the federation's financial situation was not ideal, money was "not the most important thing in a moment like this."

Lopetegui did not attend the news conference but spoke briefly with reporters as he started his trip back home.

"I am very sad, but we have a magnificent team and I hope we win the World Cup," he said.

Hierro was to lead his players for the first time in a practice session later Wednesday.

The former defender spent most of his career with Real Madrid but finished his playing days with English club Bolton in 2005. He played in four World Cups with Spain, from 1990 to 2002, and in two European Championships, 1996 and 2000.

Hierro was Carlo Ancelotti's assistant at Real Madrid after Zinedine Zidane left the post in 2014. He coached second-division club Real Oviedo two seasons ago and was Malaga's general manager after leaving his sports director position with the Spanish federation in 2011.

"I've been near a ball for 30 years," Hierro said.

After Madrid's announcement that it hired Lopetegui, critics immediately began questioning some of the coach's decisions with the national team, including his choice to leave some Barcelona players such as Sergi Roberto out of the World Cup squad. There were also questions about how Lopetegui would be able to fully focus on the national team while also having to discuss off-season signings for his new club.

There are six Madrid players in Spain's team for the World Cup.

Lopetegui's name had not been mentioned by Spanish media among the probable candidates for the Madrid job, which opened up after Zidane unexpectedly quit. Last month, Lopetegui agreed to extend his contract with the national team through 2020.

"I admire and respect Lopetegui a lot. He is a top coach, and that made it harder to make this decision," Rubiales said. "Winning is important, but above that, we need to know how things have to be handled."

It was a difficult departure for the 51-year-old Lopetegui, who took over from Vicente del Bosque after the 2016 European Championship and had been credited with reviving a team that was on the decline after winning Euro 2008, the 2010 World Cup and Euro 2012.

By successfully blending talented youngsters and veterans from its golden generation, Lopetegui kept Spain unbeaten through 20 matches in charge, comfortably leading the team to the World Cup from a qualifying group that included Italy.

He won European championships with Spain's under-19 and under-21 teams in the early 2010s, but had a lackluster stint with Portuguese team Porto in his only job with a major club.

Rubiales has already left his impression after taking over a federation which was in the hands of embattled official Angel Maria Villar for nearly three decades. He was elected president over Juan Luis Larrea, the federation's former treasurer and its interim leader since Villar was suspended following his arrest last year on suspicion of corruption.

In one of his first actions, Rubiales canceled a previously arranged "vacation" trip by officials to the World Cup that would cost nearly 2 million euros ($2.3 million). He got the money reimbursed and spent only about 500,000 euros ($590,000) with new travel arrangements for sponsors and federation officials.

"We are all affected," Rubiales said about Lopetegui's departure. "We have to think about what is best for the entity, for the federation and the team, and from this afternoon we will all be together to go forward."

___

More AP World Cup coverage: www.apnews.com/tag/WorldCup

___

Tales Azzoni on Twitter: http://twitter.com/tazzoni

Copyright 2018 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.