Warren Gatland returns as Wales coach to replace sacked Wayne Pivac
The Welsh Rugby Union said Gatland will take charge of Wales for the 2023 Six Nations and at the 2023 Rugby World Cup in France, "with the ability to go through the next World Cup cycle up to and including Australia 2027".
Pivac's exit was confirmed following a WRU review of Wales' Autumn Nations Series campaign that included a shock home defeat against Georgia.
Wales won just 13 of 34 Tests under 60-year-old Pivac - though he guided them to the 2021 Six Nations title - after he succeeded fellow New Zealander Gatland following the 2019 World Cup.
Gatland's first spell in charge, from 2008-19 saw him preside over one of Wales' most successful eras, with the team winning four Six Nations titles, including three Grand Slams, as well as reaching two World Cup semi-finals.
He stepped down after taking Wales to the last four at Japan 2019, with Gatland saying at the time: "It would break my heart if Wales went back into the doldrums."
The 59-year-old, also a former three-time British and Irish Lions head coach, will return to Wales before Christmas from New Zealand, where he was director of rugby at Super Rugby side the Chiefs.
Gatland said of his return to Wales in a WRU statement: "This is an opportunity to achieve something with a talented group of players in a country so passionate about rugby.
"Our immediate priority is obviously the 2023 Guinness Six Nations and next year's Rugby World Cup.
He added: "We will value and respect each other, we will work hard and, if we get this right together, performances and results will follow."
While July saw Wales beat the Springboks for the first time in South Africa, 2022 was a dire year overall, with just three wins from 12 Tests.
Defeat by Georgia was followed by a spectacular collapse in Cardiff against Australia, with Wales losing 39-34 after leading by 21 points in their final Autumn game.
Gatland has now returned just nine months out before Wales' World Cup opener against Fiji in Bordeaux.
He had been linked with the England head coach's job amid speculation over the future of Eddie Jones, whose position is being reviewed by the Rugby Football Union.
A decision on whether Jones stays or leaves is set to be announced within the next 48 hours.
One of the very best coaches
WRU chief executive Steve Phillips, explaining why Pivac had to go, said: "Ultimately we are in the results business and we have agreed with Wayne that the current trajectory for Wales is not where we want it to be."
Phillips, who thanked Pivac for his efforts, added: "In Warren we are bringing in one of the very best coaches in the international game.
"We know him well and, most importantly, he knows us well too. We are extremely excited about this latest chapter for Wales and Warren Gatland, and I know the feeling is mutual.
"He will undoubtedly be able to make an immediate impact, just as he did when he joined us for the first time in 2008 (when Wales won the Six Nations with a Grand Slam)."
But with Wales' regions, as well as the national side, struggling, Phillips warned: "This appointment is no quick fix, nor sticking plaster. It is part of our long-term planning for the game in Wales."
Pivac, meanwhile, said: "I am obviously extremely sad to stand down from the role."
And while insisting he had helped build a "strong foundation" for future success, Pivac added: "Unfortunately, the results or performances this year were not all as we hoped.
"As a group, we all take responsibility for that, but me in particular as head coach."