(CNN) President Donald Trump arrived in Afghanistan Thursday for a surprise Thanksgiving visit to US troops and announced peace talks with the Taliban have restarted.

It was his first trip to the country and his second visit to US troops overseas in a war zone following a trip to Iraq at the end of last year.

There are about 12,000 US troops currently serving in Afghanistan in a conflict triggered by the terrorist attacks of Sept. 11, 2001, that has claimed more than 2,300 American lives and cost billions in taxpayer dollars.

The visit came amid unprecedented tensions between the President and senior military officers following the ouster of Navy Secretary Richard Spencer after Trump intervened in the cases of three service members facing war crimes allegations against the advice of Pentagon leadership.

Speaking at Bagram Air Base, Trump told troops that "the Taliban wants to make a deal. We'll see if they want to make a deal. It's got to be a real deal, but we'll see. But they want to make a deal."

Trump also held a bilateral meeting with the President of Afghanistan, Ashraf Ghani.

Following the meeting, Ghani tweeted that "both sides underscored that if the Taliban are sincere in their commitment to reaching a peace deal, they must accept a ceasefire. We also emphasized that for any peace to last, terrorist safe havens outside Afghanistan must be dismantled."

Before leaving, Trump served Thanksgiving meals to some members of the military stationed at the base.

Hopes for a peace accord between the US and Taliban in Afghanistan were dashed when Trump announced an end to formal talks in September 2019 after a Taliban-claimed attack in Kabul that killed a dozen people, including an American soldier. Trump said at the time that Taliban leaders were to travel to the US for secret peace talks, but after the attack he called off the meetings and canceled the negotiations.

Last week, Trump seemed to suggest that negotiations with the Taliban were back on -- months after the peace talks with the militant group collapsed.

"We're working on an agreement now with the Taliban," Trump said on Fox News' "Fox & Friends." "Let's see what happens."

His comments came several days after the Taliban released an American and Australian professor in exchange for the release of three Taliban prisoners by the Afghan government. The Taliban also released 10 Afghan soldiers last week.

The US praised the release of the professors, American Kevin King and Australian Timothy Weeks, with Trump tweeting, "Let's hope this leads to more good things on the peace front like a ceasefire that will help end this long war."

On Wednesday General Mark Milley, the Chairman of the Joint Chiefs of Staff, told reporters traveling with him in Afghanistan that the prospects for successfully ending the 18-year war are higher than he has seen before.

"I think the chances of a positive outcome through negotiations is higher than I have seen, and I've been deeply involved in Afghanistan for 18 years, he told reporters Wednesday according to Reuters. "With a bit of luck, we'll have successful negotiations in the near term, not too distant future."

Tensions with military leadership

The visit follows Trump's controversial decision earlier this month to break with military leaders and intervene in the cases of three service members facing war crimes allegations including Navy SEAL Eddie Gallagher.

Gallagher had been convicted of bringing discredit to the armed services after posing next to a dead ISIS fighter's body, which is against regulations. He was demoted for that offense and acquitted of a separate murder charge.

Earlier this month, Trump reversed Gallagher's demotion and later tweeted that he wouldn't let the Navy punish Gallagher in its ongoing review of whether he should be kicked out of the SEALs.

That move ultimately led to the ouster of Spencer who was "fired" by Secretary of Defense Mark Esper for going outside his chain of command by proposing a "secret agreement with the White House," according to a senior defense official.

Spencer appeared to be seeking a way to resolve a standoff between the Pentagon and the White House over Gallagher's case.

On Wednesday, Spencer issued a stunning rebuke of the President's involvement in the Gallagher case, calling his actions "shocking and unprecedented" in a blistering Washington Post op-ed published days after he was forced to resign.

"It was also a reminder that the president has very little understanding of what it means to be in the military, to fight ethically or to be governed by a uniform set of rules and practices," he wrote.

Dismay in the Pentagon has been building over Trump's sporadic, impulsive and contradictory decision-making on a range of issues, including his sudden pullback of troops in Syria.

But CNN reported Wednesday that there are new and significant worries, as multiple military officials and retired officers say Trump's intervention into high-profile war crimes cases cannot be ignored.

A long-serving military officer put it bluntly, telling CNN "there is a morale problem," and senior Pentagon officials have privately said they are disturbed by the President's behavior.

Trump had upped the ante at a rally on Tuesday by issuing an extraordinary declaration that he took action in the face of "deep state" opposition. In fact, senior Pentagon officials had been unanimously opposed to the President's intervention because they believed it would undermine military discipline and order.

CNN has learned that at least two senior military officers were reluctant to appear alongside Trump at events in recent months, because of unease that he might make partisan political remarks while they were present. CNN has been asked by sources close to both officers not to identify them or the events involved.

Several senior commanders are considering writing a memo to the troops reminding them of their moral and legal responsibilities on the battlefield. The matter is so sensitive that some are privately indicating they want high level Pentagon approval before they proceed because the memo could appear to be a rebuke of the President.

CORRECTION: This story has been corrected to reflect that Melania Trump did not travel with the President. It has also been updated with additional developments.