WASHINGTON — For all of President Donald Trump’s talk about how much he loves his military generals, those picked for prominent roles in his orbit have not tended to fare well.

The plight of Trump’s generals is coming into focus as beleaguered chief of staff John Kelly, a retired Marine Corps general, is on his way out. Trump is also causing a stir at the Pentagon by undermining Defense Secretary Jim Mattis, himself a retired general, with his untimely selection of Gen. Mark Milley as the next Joint Chiefs of Staff chairman — against Mattis’ advice.

Even as a candidate, Trump made clear his strong affinity for decorated senior military leaders, whose presence in proximity to Trump seemed to lend a certain muscular credibility to his boasts about acting with great strength. So it wasn’t a surprise that he’s picked many of them for top national security roles in his administration.

“See my generals, those generals are going to keep us so safe,” Trump said at his inaugural luncheon, gesturing toward Gens. John Kelly and James Mattis at the Capitol. “These are central casting. If I’m doing the movie, I’d pick you.”

It’s not uncommon historically for presidents to tap uniformed leaders for a few top roles such as the national security adviser. But retired Army Gen. Barry McCaffrey, an NBC News national security analyst, said it was obvious from the start that Trump “clearly has become fixated on military officers.”

“He admires them, he wants to be one,” McCaffrey said. But, he added, “You put them in that White House, and they rapidly get compromised by that situation.”

As president, Trump made good on placing high-ranking military leaders in top roles, including both of his first two national security advisers: retired Lt. Gen. Michael Flynn and Lt. Gen. H.R. McMaster. Flynn was fired after three weeks and later pled guilty to lying to the FBI as part of the Russia probe. His replacement, McMaster, grew at odds with the president on a host of key foreign policy issues and was removed in March.

Gen. Mark A. Milley speaks during an activation ceremony for the U.S. Army futures Command on Aug. 24, 2018, in Austin, Texas. Eric Gay / AP file

Now Kelly, who was also Trump’s first Homeland Security secretary, is the next to go. He will “be leaving at the end of the year,” Trump casually told reporters Saturday, after weeks of speculation and reported tensions inside the West Wing. During Kelly’s term as chief of staff, his growing frustrations with Trump had increasingly surfaced in news headlines, amid his struggles to impose structure in a White House in which decision-making is haphazard and the president’s family has channels of influence that circumvented his.

Asked whether Kelly had resigned or was forced out, Larry Kudlow, Trump’s top economic adviser, told "Fox News Sunday," “I don’t know, to be perfectly honest.”

For Mattis, serving in Trump’s Cabinet seemed to work well initially despite early concerns raised about maintaining civilian control over the military. Sensitive to those concerns, Mattis’ staff knew to call him Secretary, never General Mattis. And they knew never to refer to him as “Mad Dog,” a moniker he disliked and that came from reporters who covered Mattis as a general officer leading Marines in war zones. Mattis’ actual nickname is Chaos, an acronym for Colonel Has An Outstanding Suggestion.

But Trump, seemingly unaware or unconcerned, frequently refers to Mattis in public as “General” or “Mad Dog.”

Mattis, once a four-star Marine Corps general, was the rare adviser who could occasionally persuade Trump not to indulge his own instincts. He helped keep Trump from withdrawing from the Iran nuclear deal for many months until Trump ultimately did in May, and held at bay a hasty U.S. withdrawal from Syria.

But as Trump has grown into the roles of president and commander in chief, he’s been less and less inclined to defer to the advice of others — including decorated military leaders.

During a July 2017 meeting in the Situation Room, Trump repeatedly ordered Mattis to fire the commander in Afghanistan, Gen. John Nicholson, because he was not winning the war. Mattis and Gen. John Dunford, the chairman of the Joint Chiefs, both defended Nicholson, with Dunford offering to set up a meeting between Nicholson and Trump since they had never met.

"Despite an initial infatuation with military leaders, the president seems to be tiring of them,” said retired Navy Adm. James Stavridis, the former NATO supreme allied commander and chief international security analyst for NBC News. “I fear General Mattis may be next, which would be the worst blow of all."

Trump eventually did pull out of the Iran deal and has overruled Mattis on creation of a “space force” branch and on concessions to North Korea.

“I think he’s sort of a Democrat, if you want to know the truth,” Trump told CBS’ “60 Minutes” in October in a clear swipe at Mattis, who Trump predicted might leave the administration.

Now Trump is rejecting the advice of Mattis by nominating Milley, currently the Army chief of staff, to replace Gen. Joseph Dunford as chairman of the Joint Chiefs.

Mattis had recommended Gen. David Goldfein of the Air Force for the job, two Defense Department officials and two former senior military officials tell NBC News. Trump interviewed both Goldfein and Milley in November.

“A lot of people interpret the selection as a shot across Mattis’ bow,” said a former senior defense official.

Trump’s selection caught top military brass — including Mattis, Dunford and Milley — off guard. They didn’t know Trump was going to announce his nomination for the next chairman until the president previewed it to the press Friday morning.

Even then, Pentagon and military leaders scrambled to figure out what Trump was going to announce. A day later, military leaders were surprised yet again when the White House informed them that Trump planned to tweet his decision early Saturday, officials said.

Trump said that Dunford “will be retiring” but did not say when. He said the date of transition was “to be determined.”

The timing itself was a serious breach of protocol, given that Dunford isn’t scheduled to retire from the job until October 2019. Traditionally, the announcement of the next joint chiefs chairman doesn’t take place until the spring.

Dunford doesn’t intend to leave early, the two current officials said. But that means an awkward 10 months as a lame duck, with his named replacement waiting in the wings.

Three U.S. defense officials said Dunford has had a good relationship with Trump, and none were aware of any major disagreements or frictions that would lead Trump to push him out early.

Dunford was nominated for the post by President Barack Obama and renewed by Trump. During the 2016 presidential campaign, Trump had hinted that he’d remove military leaders who’d been appointed by Obama.

“Under the leadership of Barack Obama and Hillary Clinton, I think the generals have been reduced to rubble,” Trump said during a September 2016 “Commander in Chief” forum on NBC News. He later suggested he’d seen advice from his generals in defeating the Islamic State group but added, “They’d probably be different generals.”

The official response from Mattis’ office to Trump’s selection of Milley was notably lukewarm, devoid of the customary praise for the man who will become the highest-ranking military officer in the country.

"We are aware of the President's nomination and share his confidence for Gen. Mark Milley,” said Lt. Col. Mike Andrews, a Defense Department spokesman.

To be sure, senior military leaders aren’t the only ones who have had rough rides in Trump’s administration that have often ended in collapse. Rex Tillerson was unceremoniously fired as the secretary of state on Twitter in March only to be called “dumb as a rock” by the president on Friday. Former Attorney General Jeff Sessions endured months of public humiliation by the president before ultimately being forced out last month.

But as Trump approaches the second half of his term, the poor fortunes of most of those who have risked serving in senior roles so far has raised the question of whether he’ll be able to attract top talent to work for him going forward. Several of Trump’s most experienced allies have expressed reluctance either privately or publicly to fill key vacancies in his administration.

A glaring reminder of that dynamic came Sunday night when Nick Ayers, Vice President Mike Pence’s chief of staff, turned down Trump’s offer to have him replace Kelly as chief of staff, despite Trump’s fondness for Ayers and the rising influence of Pence’s team within the White House. Another top Pence aide, retired Lt. Gen. Keith Kellogg, had strongly pushed Trump to pick Milley over Goldfein, two defense officials said, an argument that ultimately won out.