WASHINGTON — Lt. Gen. H. R. McMaster, the battle-tested Army officer tapped as President Trump’s national security adviser last year to stabilize a turbulent foreign policy operation, will resign and be replaced by John R. Bolton, a hard-line former United States ambassador to the United Nations, White House officials said Thursday.

McMaster will retire from the military, the officials said. He has been discussing his departure with President Trump for several weeks, they said, but decided to speed up his departure, in part because questions about his status were casting a shadow over his conversations with foreign officials.

The officials also said that Trump wanted to fill out his national security team before his meeting with North Korea’s leader, Kim Jong-un. He replaced Secretary of State Rex W. Tillerson with the C.I.A. director, Mike Pompeo, last week.

Officials emphasized that McMaster’s departure was a mutual decision and amicable, with none of the recrimination that marked Tillerson’s exit. They said it was not related to a leak on Tuesday of briefing materials for Trump’s phone call with President Vladimir V. Putin of Russia.

In the materials, Trump was advised not to congratulate Putin on his re-election, which the president went ahead and did during the call.

Bolton, who will take office April 9, has met regularly with Trump to discuss foreign policy, and was on a list of candidates for national security adviser. He was in the West Wing with Trump to discuss the job on Thursday.

“H.R. McMaster has served his country with distinction for more than 30 years. He has won many battles and his bravery and toughness are legendary,” Trump said in a statement. “General McMaster’s leadership of the National Security Council staff has helped my administration accomplish great things to bolster America’s national security.”

McMaster had struggled for months to impose order not only on a fractious national security team but on a president who resisted the sort of discipline customary in the military. Although McMaster has been a maverick voice at times during a long military career, the Washington foreign policy establishment had hoped he would keep the president from making rash decisions.

Yet the president and the general, who had never met before Trump interviewed McMaster for the post, had little chemistry from the start, and often clashed behind the scenes.

McMaster’s serious, somber style and preference for order made him an uncomfortable fit with a president whose style is looser, and who has little patience for the detail and nuance of complex national security issues. They had differed on policy, with General McMaster cautioning against ripping up the nuclear deal with Iran without a strategy for what would come next, and tangling withTrump over the strategy for American forces in Afghanistan.

Their tensions seeped into public view in February, when McMaster said at a security conference in Munich that the evidence of Russian interference in the 2016 presidential election was beyond dispute. The statement drew a swift rebuke from the president, who vented his anger on Twitter.

“General McMaster forgot to say that the results of the 2016 election were not impacted or changed by the Russians and that the only Collusion was between Russia and Crooked H, the DNC and the Dems,” Mr. Trump wrote, using his campaign nickname for Hillary Clinton. “Remember the Dirty Dossier, Uranium, Speeches, Emails and the Podesta Company!”

Trump selected McMaster last February after pushing outMichael T. Flynn, his first national security adviser, for not being forthright about a conversation with Russia’s ambassador at the time. (Flynn has since pleaded guilty of making a false statement to the F.B.I. and is cooperating with Robert S. Mueller III, the special counsel investigating Russia’s interference in the 2016 election.)

McMaster carried out a slow-rolling purge of hard-liners at the National Security Council who had been installed by Mr. Flynn and were allied ideologically with Stephen K. Bannon, Trump’s former chief strategist, earning the ire of conservatives who complained that his moves represented the foreign policy establishment reasserting itself over a president who had promised a different approach.

McMaster’s position at the White House had been seen as precarious for months, and he had become the target of a concerted campaign by hard-line activists outside the administration who accused him of undermining the president’s agenda and pushed for his ouster, even creating a social media effort branded with a #FireMcMaster hashtag.

Last summer, Trump balked at a plan General McMaster presented to bolster the presence of United States forces in Afghanistan, although the president ultimately embraced a strategy that would require thousands more American troops.

McMaster had been among the most hard-line administration officials in his approach to North Korea, publicly raising the specter of a “preventive war” against the North. He was among those who expressed concerns about Mr. Trump’s abrupt decision this month to meet Kim Jong-un, according to a senior official.