WASHINGTON — President Donald Trump will nominate Patrick Shanahan to be the next head of the Pentagon after more than four months of internal deliberations — and a golf course chat with Sen. Lindsey Graham, officials familiar with the matter told NBC News.

A former Boeing executive with no military experience, Shanahan was thrust into the role of acting Pentagon chief at the start of this year, after Trump ousted James Mattis two months earlier than Mattis planned to step down.

But Shanahan was not Trump's first choice to lead the Department of Defense, the officials said. And there were times over the past four months that, despite the president's public praise for Shanahan, it was far from certain he would get the job. One of the holdups was Trump's concern that Shanahan does not fit the mold of a battle-hardened general, according to the officials.

One person who has advised the president on the decision said that Trump doesn't see Shanahan as someone who carries himself the way he thinks a defense secretary should. In particular, he doesn't think Shanahan has a strong presence in person or, perhaps more important for this president, on television. But Trump also was reluctant to make a change because Shanahan was already there.

Indeed, Trump was still seeking advice as recently as last week about whether to nominate him, according to officials familiar with the process.

Ultimately, two men who Trump wanted to pick for the job actually helped him select Shanahan: Graham, the South Carolina Republican senator, and retired Gen. Jack Keane. Trump eyed both Graham and Keane for the defense secretary role but neither was interested, the officials said.

Graham has privately expressed opposition to Shanahan for months. But in recent weeks, Shanahan, who already had the support of influential officials such as National Security Adviser John Bolton, was able to win over Graham and Keane, as the White House signaled he was the president's leading choice.

One factor that may have influenced Trump's decision on Shanahan was a discussion with Graham on April 28 over a round of golf at his club in Virginia, officials said.

A spokesman for Graham declined to comment.

President Trump intends to nominate Patrick M. Shanahan as Secretary of Defense. pic.twitter.com/RaOXb09qMr — Sarah Sanders (@PressSec) May 9, 2019

In the days before the White House said Trump intended to nominate Shanahan for the top Pentagon job, Keane expressed his support for the acting defense secretary.

"Based on my discussions with Acting Secretary Shanahan and observing him in that capacity, he is definitely qualified to serve as Secretary of Defense," Keane said.

Graham clashed with Shanahan early into his tenure as acting secretary, most notably during a closed-door briefing in February, where the South Carolina Republican questioned Shanahan on Trump's decision to withdraw troops from Syria. As the discussion got heated, Graham declared himself Shanahan's "adversary."

But Keane, a former four-star Army general and Fox News analyst, helped convince Graham to endorse Shanahan, according to officials familiar with the discussions.

Keane has influenced Trump's decisions in the past, most notably convincing him to reverse a decision to no longer allow family members of service members deployed in South Korea to live there. The Pentagon did not ultimately change the policy.

Shanahan won over Keane during a meeting with him at the Pentagon early last week, according to officials familiar with the meeting. The two met for more than an hour. Keane then spoke with Graham about Shanahan several times in advance of the senator's golf outing with Trump.

On Sunday, April 28, at Trump National Golf Club in Sterling, Va., Graham made the argument to the president that the Pentagon needs stability and he should nominate Shanahan soon. Graham gave Shanahan a "strong endorsement," and convinced Trump he needs a permanent secretary of defense now, according to two people familiar with the conversation.

Graham, one of Trump's strongest supporters and most trusted confidantes, appeared to push president toward a final decision, according to officials familiar with the discussions.

The next day, Shanahan had a one-on-one meeting with Trump in the Oval Office in which they discussed the job, the officials said.

But Trump still waited another 10 days before announcing he was nominating Shanahan to the job.

During a meeting with reporters on Capitol Hill this week, the chairman of the Senate Armed Services Committee, Sen. Jim Inhofe, R-Okla., said he met with the president for over an hour last week and tried to convince him to move forward with Shanahan. "The most important thing is we get someone confirmed and in place and get rid of the word 'acting.' You can't get anything done overseas if you're acting anything," he said.

Inhofe could not explain the delay in the nomination. "There's some obstacle that's making it take longer and I don't know what that obstacle is."