When Nikki Haley was governor of South Carolina in 2016, she said she was appalled by Donald J. Trump’s threat to ban all Muslims from entering the United States should he become president. Ms. Haley, herself the child of Indian immigrants, called the pledge “absolutely un-American,” and part of a pattern of “unacceptable” comments and acts.Just two days after she joined Mr. Trump’s new administration in January 2017 as ambassador to the United Nations, she had to confront the issue anew. Mr. Trump barred travelers and refugees from seven Muslim-majority countries from entering the United States for 90 days.At a hastily called White House meeting, other senior administration officials objected, saying the prohibition would endanger refugees already en route to the United States and would hurt families of Iraqis who had long worked closely with the American military in that nation.“I don’t remember Nikki Haley saying anything,” said Kristie Kenney, then a top State Department official, who sat in on meeting. Six weeks later, in one of her first interviews as ambassador, Ms. Haley defended the ban, saying it was directed against countries with terrorist activity, not against Muslims.Now, as she tries to persuade Republican voters to cast Mr. Trump aside and hand her the mantle, Ms. Haley is reverting to her role as Trump critic. As her bid for the White House has picked up steam, she has warned voters that “we cannot have four years of chaos, vendettas and drama,” an obvious reference to his White House years. “America needs a captain who will steady the ship, not capsize it,” she added. Unlike Mr. Trump, she has said, she would not praise dictators and would “have the backs of our allies.”But when Ms. Haley had a chance to influence Mr. Trump, she chose her battles carefully. In interviews with more than a dozen former senior administration officials, most said that while Ms. Haley at times expressed her views frankly, they rarely witnessed her going to the mat, as some other senior aides did, to try to head off or moderate what they saw as Mr. Trump’s rash moves.Ms. Haley made herself a reliable defender of the president to the outside world, often trying to soften the edges of his most abrasive decisions. Privately, she carefully guarded what she later called her “amazingly good relationship” with Mr. Trump and avoided some of the internal fights that would have pitted her against him.“I don’t pick up the phone and say, ‘What are you doing?’” she said in an interview in March 2017, acknowledging that she was at times taken back by some of his public statements. “I just know that’s who he is.”Ms. Haley’s former colleagues could not recall her in the forefront of fights to keep Mr. Trump from imposing trade tariffs on American allies, or rushing into an unprecedented summit with North Korea’s dictator, or canceling America’s longstanding military exercises with South Korea, or banning Iraqis from entering the country. It fell mainly to others to defend NATO from Mr. Trump’s attacks, they said. Many spoke on the condition of anonymity in order to describe internal deliberations.“I think that Haley understood, in an almost visceral way, the importance of maintaining a good relationship with the president,” said Thomas A. Shannon Jr., who served as under secretary of state for political affairs for the first half of Ms. Haley’s tenure. “She did not take on this job to do battle with the president.”Not everyone agrees that she held her fire. “Nikki Haley never pulled any punches with Donald Trump or with anybody,” said H.R. McMaster, Mr. Trump’s national security adviser from early 2017 to early 2018 and a key ally of Ms. Haley. “Oftentimes, she told him what he didn’t want to hear.”That is the impression Ms. Haley is trying to make with voters, as she casts herself as no-nonsense, no-drama alternative to Mr. Trump, who leads in polls in Iowa by some 30 percentage points. “If he was doing something wrong, I showed up in his office or I picked up the phone and said you cannot do this,” she said last week in Wolfeboro, N.H.Both Mr. McMaster and Ms. Haley point to her stance on Russia as evidence that she stood up to Trump. In her 2019 memoir on her U.N. tenure, Ms. Haley said she phoned the president directly to complain that he was overly deferential to Russian President Vladimir V. Putin in a July 2018 meeting, telling him: “The Russians aren’t our friends.”Asked to point to other examples, her campaign did not respond. Nor did her aides answer questions about whether and how she used her influence with the president on a variety of issues that galvanized other senior administration officials.There were clear dividends to keeping Mr. Trump’s favor. The ambassadorship allowed Ms. Haley, who had never held office outside of South Carolina, to gain valuable foreign policy experience and to build the political brand that she now hopes will carry her to the White House.She also achieved a rare graceful exit from the administration, escaping the public insults the president rained on so many of his top aides. Instead, he praised her as “fantastic.”Staying on Trump’s Good SideMs. Haley has looked to cast herself as a no-drama alternative to Mr. Trump.Maansi Srivastava/The New York TimesMs. Haley’s position gave her the luxury of distance from some scorching White House debates. Other senior administration officials recalled sprinting to the Oval Office to try to forestall some of Mr. Trump’s orders. Stationed in New York, answering to a president who cared little about the United Nations, Ms. Haley was to some degree on the periphery.Nonetheless, she had unusually good access to the president. Mr. Trump had granted her wish to be seated on the National Security Council, over the objections of Secretary of State Rex Tillerson, to whom she ostensibly reported. And he took her calls, which former Trump aides described as frequent.Because the Trump White House operated in an unconventional fashion, often without the customary briefing papers and deliberate discussions, senior administration officials created unusual and shifting alliances in hopes of influencing the president. They tried to rope in like-minded officials, even on issues outside their portfolio.Several former senior administration officials said they did not view Ms. Haley as a useful ally in countering Mr. Trump because they thought she was unlikely to challenge the president directly. That was the case, they said, in the effort to keep Mr. Trump from imposing steel and aluminum tariffs against American allies like Canada. Gary D. Cohn, the White House economic adviser, led that fight, backed by a group that included Defense Secretary Jim Mattis and Mr. McMaster, but not Ms. Haley.Nor was she central to the endeavor by other senior foreign policy advisers to take Iraq off the list of seven Muslim-majority nations covered by Mr. Trump’s travel ban. Mr. McMaster, Mr. Tillerson, Mr. Mattis and John F. Kelly, then head of homeland security, argued that the ban would punish Iraqis who for over a decade worked with the U.S. government to fight extremists.In a series of heated White House meetings, ending up in the Oval Office, they faced off against the White House advisers Steve Bannon and Stephen Miller, finally swaying the president to their side. While Mr. McMaster said Ms. Haley agreed Iraq should be dropped from the list, others who described those meetings make no mention of her.Guy Snodgrass, Mr. Mattis’s former chief speechwriter, said he knew of conversations in which the defense secretary and other senior officials discussed how best to influence the president. But he was not aware, he said, of any interaction with Ms. Haley or her staff.Ms. Haley was viewed as having shrewd political instincts — and also clear aspirations beyond the United Nations. Mr. Trump was wary of her ambitions, according to people familiar with his views. Some thought she tended carefully to her relationship with the president partly to safeguard them.“I thought she went out of the way not to take Trump on. Her objective, I thought, was to stay on his good side,” said John R. Bolton, who succeeded Mr. McMaster as national security adviser in March 2018.In her memoir, Ms. Haley recounted one instance, apparently in late 2017, when Mr. Tillerson and Mr. Kelly, then White House chief of staff, tried to enlist her support in holding the president in check. While they claimed that they needed to band together for the good of the country, she wrote, she saw them as disloyal.Ms. Haley later told Fox News that she reported the conversation to Mr. Trump and Mr. McMaster. Mr. McMaster said in an interview that she understood the importance of duty.Mr. Tillerson has denied ever trying to undermine the president. Mr. Kelly has said that he gave the president the best advice he could.Dealing With DictatorsMs. Haley called Mr. Trump to criticize his 2018 meeting with President Vladimir V. Putin of Russia in Helsinki.Doug Mills/The New York TimesMs. Haley has written that she agreed with most of Mr. Trump’s major policies, including his decisions to withdraw from the Iran nuclear deal and abandon the Paris climate accord. His posture toward Russia, however, was a steady source of friction.One former senior official said that the only times the president would become angry with Ms. Haley were when she criticized Mr. Putin in public, and that he would order his chief of staff to tell her to stop.Still, she called Mr. Trump to complain about his 2018 summit in Helsinki, where the president had ignited a bipartisan uproar by suggesting he believed Mr. Putin’s denials of Russian interference in the 2016 U.S. elections over the assertions of U.S. intelligence agencies. “You made it sound like we were beholden to them,” she said she told him.Later that year, she persuaded the president to toughen up the administration’s talking points after Russian forces seized three Ukrainian naval ships and threatened to turn the Sea of Azov into a Russian lake, according to Mr. Bolton’s memoir.But even as she objected to Mr. Trump’s approach toward Mr. Putin, she has excused it. In her book, she wrote that she understood why he seemed to let Mr. Putin off the hook in Helsinki. “He was trying to keep communication open with Putin, just as he had with Kim Jong-un and Chinese President Xi Jinping,” she wrote, then went on to extol his ability to disarm people.Similarly, Ms. Haley suggested that Mr. Trump meant well when he praised Mr. Kim as a “talented” leader who “loves his people” and that he just didn’t understand how his words would be received.Since starting her campaign, Ms. Haley has said Mr. Trump “was too friendly” with Mr. Kim, “a thug and a tyrant” who has been “terrible to his people.” (One of Ms. Haley’s biggest accomplishments as ambassador was garnering support from Russia and China for a series of economic sanctions against North Korea after it conducted a battery of missile tests.)Mr. Trump himself has noticed her frequent oscillations: “Every time she criticizes me, she uncriticizes me about 15 minutes later,” he told Vanity Fair in late 2021. “I guess she gets the base,” referring to his popularity with Republican voters she is now courting.‘We Shouldn’t Have Followed Him’Ms. Haley had promised in early 2021 not to run against Mr. Trump for the Republican presidential nomination.Samuel Corum for The New York TimesIn trying to explain why she is so much more critical of him now than before, Ms. Haley has said it is Mr. Trump who has changed, not her.As late as December 2020, after Mr. Trump lost the presidency to Joseph R. Biden Jr., Ms. Haley still took a forgiving stance toward him. She told Politico that although she spoke with Mr. Trump after the election, she did not urge him to concede because he sincerely believed he had won and couldn’t be convinced otherwise. It was a version of the “that’s who he is” argument she had made when she first joined his administration.Then, after his supporters ransacked the Capitol in January 2021, she told Politico there were no excuses for his behavior. “He went down a path he shouldn’t have, and we shouldn’t have followed him,” she said.After she announced this February that she would run against him for the presidential nomination — after promising not to in early 2021 — a political action committee supporting Mr. Trump’s campaign characterized her as an opportunist, “only in it for herself.”Ms. Haley addressed that kind of criticism in an essay in The Wall Street Journal in 2021. She wrote that Mr. Trump had been a good president but had gone astray, and said she could not “defend the indefensible.”“If that means I want to have it ‘both ways,’” she added, “so be it.”Reporting was contributed by More