In this May 17, 2017, file photo, President Donald Trump talks with Homeland Security Secretary John Kelly during commencement exercises at the U.S. Coast Guard Academy in New London, Conn. Trump named Kelly as his new Chief of Staff on July 28, ousting Reince Priebus.

White House Director of Social Media Dan Scavino, left, walks with former White House Chief of Staff Reince Priebus as they step off Air Force One as they arrives Friday, July 28, 2017, at Andrews Air Force Base, Md. Trump says Homeland Secretary John Kelly is his new White House chief of staff.

WASHINGTON >> His White House in turmoil, President Donald Trump abruptly announced late Friday he was appointing Homeland Security Secretary John Kelly to be his chief of staff, ending the tumultuous six-month tenure of Reince Priebus.

After months of speculation about Priebus’ fate, Trump tweeted his decision as he landed in Washington after a speech in New York in which he lavishly praised Kelly’s performance at Homeland Security.

Priebus, the former Republican National Committee head, had been a frequent target of rumors about his job security amid infighting and confusion within the White House and a long whisper campaign by Trump allies. Then, on Thursday, he was assailed in a remarkable and profane public rebuke by Trump’s newly appointed White House communications director, Anthony Scaramucci.

Priebus said he had offered his resignation on Thursday and the president accepted though those close to the president said the ouster had been in the works for weeks.

“I think the president wanted to go a different direction,” Priebus told CNN just hours after his exit was announced. He added that he agreed the White House might well benefit from “a reset,” and he said, “I’m always going to be a Trump fan. I’m on Team Trump.”

Trump’s announcement on Twitter said, “I am pleased to inform you that I have just named General/Secretary John F Kelly as White House Chief of Staff. He is a Great American … and a Great Leader. John has also done a spectacular job at Homeland Security. He has been a true star of my Administration.”

He also saluted Priebus, the chief of staff he had just pushed out.

“I would like to thank Reince Priebus for his service and dedication to his country. We accomplished a lot together and I am proud of him!”

Kelly is a retired Marine four-star general though he has little experience in civilian government or the legislative process. Trump had focused on him in recent days, telling those close to him that he loved the general’s star power and that he believed military discipline was what his administration needed.

Priebus never could bring a semblance of order to the team of in-fighting rivals that populate Trump’s West Wing, and questions about his future have long swirled around the office. Those questions sharply escalated this week with the arrival of Scaramucci, the hard-charging communications director who was hired over Priebus’ objections.

Priebus’ already tense relationship with Scaramucci took a darker turn over the past two days when the communications chief suggested in a late-night tweet that Priebus was one of the “leakers” that Trump has railed against. The New Yorker magazine published an interview Thursday in which Scaramucci called Priebus, amid an avalanche of vulgarity, a “paranoid schizophrenic.”

Priebus, who hails from Wisconsin and has deep ties to House Speaker Paul Ryan, had grown increasingly isolated in the White House, as past Republican National Committee colleagues and other allies have left or been pushed out. Those who have departed include former deputy chief of staff Katie Walsh, former communications chief Mike Dubke, press secretary Sean Spicer and press aide Michael Short.

Another early departure from the Trump White House was National Security Adviser Michael Flynn, and Trump ousted FBI Director James Comey early on. He has lobbed Twitter insults at Attorney General Jeff Sessions recently though Sessions is still in place.

Ryan, in a statement, said Priebus “has left it all out on the field, for our party and our country.” Ryan added that he looked forward to working with Kelly.

Both Scaramucci and Priebus traveled to New York’s Long Island with Trump on Friday for a speech in which the president highlighted efforts to crack down on the gang MS-13. The chief of staff took the return flight to Washington, his fate sealed in the tweets that were sent by the president just as Priebus stepped off the plane.

Shortly before the president deplaned, Priebus’ black SUV pulled away, leaving the rest of the motorcade, including the president’s vehicle, in the distance. The president eventually emerged, umbrella in hand, and delivered a brief statement on the runway as driving rain poured.

Rep. Peter King of New York sat across from the outgoing chief of staff on Air Force One’s return flight to Washington and said Priebus “kept a poker face.”

Priebus’ term ends in fewer than 200 days, the shortest tenure for any president’s first White House chief of staff since the post was formally established in 1946. From day one, his power has been limited compared with past officials with his title. In a highly unusual arrangement, Trump said at the outset that Priebus and chief strategist Steve Bannon would serve as “equal partners” in implementing his agenda.

Scaramucci was the latest top aide to be granted a direct line to Trump, and it became increasingly unclear who actually reported to Priebus. Though Priebus forged an uneasy truce with his former foe Bannon, powerful White House aides Ivanka Trump and her husband, Jared Kushner, were both supportive of Kelly’s hire.

Priebus was blamed by some within the White House for the failure of the Republican health care plan, with some Trump allies believing that Priebus’ longtime relationships with Republicans on Capitol Hill should have ensured the bill’s passage.

Though Priebus had told allies he hoped to stay, many around Trump had been working on his departure for quite some time.

The final go-ahead came during a meeting Thursday night in which Trump finalized that Kelly was his pick to replace Priebus, according to two people familiar with the discussion The president had considered others, including political operatives David Urban and Wayne Berman, for the job. But he decided on Kelly because he was impressed by the job he had done leading DHS and trusted him not to leak.

Priebus, a political operative and attorney, is expected to look for a corporate job or possibly write a book about his experience in the center of the Trump storm. One of the final establishment Republicans in the White House, he was a frequent target of barbs from Trump over not being an early backer of the celebrity businessman’s candidacy.

As Homeland Security secretary, Kelly has taken the lead on some of Trump’s most controversial policies, including his executive orders suspending the admission of refugees and temporarily barring visitors from several Muslim-majority nations. Those orders have been stripped down by courts pending a Supreme Court review this fall.

People who know Kelly told The Associated Press that he was not aware of the details of those initial orders until around the time that Trump signed them. Yet, just days after taking office, Kelly had to lead the agency as it dealt with the chaos and confusion that ensued at airports in the U.S. and around the world. He defended the orders to reporters and lawmakers and insisted that he indeed had been part of the decision-making process.

Republican Sen. Lindsey Graham of South Carolina, a frequent Trump critic, welcomed Kelly as “one of the strongest and most natural leaders I’ve ever known.”