The Senate voted Thursday to confirm Gina Haspel as the next CIA director after several Democrats were persuaded to support her despite lingering concerns about the role she played in the brutal interrogation of suspected terrorists captured after 9/11.

Lawmakers approved Haspel’s nomination 54 to 45, with six Democrats voting yes and two Republicans voting no, after the agency launched an unprecedented public relations campaign to bolster Haspel’s chances. She appears to have been helped, too, by some last-minute arm-twisting by former CIA directors John Brennan and Leon Panetta, who contacted at least five of the six Democrats to endorse her bid to join President Trump’s Cabinet, according to people with knowledge of the interactions.

Trump had wavered in his support for Haspel, at times expressing doubt in private meetings about whether she had the support to win confirmation, according to people with knowledge of the matter. Earlier this month, Haspel sought to withdraw after some White House officials worried her involvement in the CIA’s interrogation program could derail her chances.

Trump decided to push for Haspel to stay in the running, after first signaling he would support whatever decision she made, administration officials said.

Haspel has not had as close of a relationship with Trump as the CIA’s previous director, Secretary of State Mike Pompeo, who is one of the president’s closest advisers, according to people with knowledge of Haspel and Trump’s interactions. But she has been successful, to a degree, influencing his stance toward Russia, whose aggressive and adversarial posture toward the West has become a top national security priority for the administration.

Following a nerve-agent attack in Britain that American and British officials blamed on the Russian government, Haspel argued for a forceful response, which ultimately led to the United States expelling 60 Russian intelligence operatives and shuttering a Russian consulate in Seattle, people with knowledge of her role said. Haspel was a leading player in the multi­agency response to the attack and advised the president to make a bold demonstration to counter Russia and stand with Britain, the United States’ closest intelligence ally, these people said.

The president posted a congratulatory tweet after the confirmation vote.

Haspel’s ascent to the top post in the nation’s most storied spy service says much about the CIA’s past and its future.

She will be the first woman to serve as director. When Haspel joined the CIA in 1985, there were fewer opportunities for women to live the life of a cloak-and-dagger operative that she found alluring. Haspel took a posting as a field officer in Ethiopia, an unglamorous assignment, but one that taught her how to run operations against agents for the Soviet Union, then a benefactor of the Ethiopian government.

Her first assignment as chief of station, in Baku, Azerbaijan, in 1996, prompted skepticism from male colleagues, who thought the CIA shouldn’t send a woman to such a remote and rough location, according to people who worked with Haspel.

Haspel’s request for a transfer to the CIA’s Counterterrorism Center proved to be a fateful move. Her first day on the job was Sept. 11, 2001, and she became an integral part of the CIA’s early operations against al-Qaeda, according to current and former colleagues. At the time, counterterrorism was also a less coveted assignment, but an area where women were getting significant jobs and excelling at them.

In her bid to become the next director, Haspel and her supporters emphasized the historic nature of her nomination and how her career tracked with the rise of women in the intelligence services.

“It is not my way to trumpet the fact that I am a woman up for the top job, but I would be remiss in not remarking on it — not least because of the outpouring of support from young women at CIA who consider it a good sign for their own prospects,” Haspel told senators at her confirmation hearing last week.

After Thursday’s vote, Director of National Intelligence Daniel Coats called Haspel a “trailblazer,” praising the mix of “front-line and executive experience” she has accumulated over a long career at the agency.

“Her confirmation represents the best we have to offer as a country,” he said.

But it is the dark chapters of Haspel’s past — and that of the CIA — that imperiled her nomination from the start and will not be closed as she takes over at the agency’s headquarters in Langley, Va.

Throughout the debate over her nomination, Haspel and her supporters struggled to reconcile her portrayal as a capable and forceful leader, but someone who lacked the authority to stop the interrogation program or overrule her boss’s decisions to order harsh interrogations and then destroy videotaped evidence.

Critics said she lacked the will to do so, and were unpersuaded that she had learned a moral lesson from the agency’s torture of terrorism suspects — a program that was disbanded but that Trump has said should be restarted.

In late 2002, Haspel, then a senior leader in the Counter­terrorism Center, managed a secret detention facility in Thailand where two al-Qaeda suspects were waterboarded (one of them before Haspel’s arrival).

Laura Pitter, a national security counsel at Human Rights Watch, called Haspel’s confirmation “the predictable and perverse byproduct” of the country’s failure to come to terms with past abuses.

“The torture at the center of the CIA’s rendition, detention, and interrogation program was a crime plain and simple, but the U.S. government has never been willing to admit that or to take appropriate action,” she said. “Until it does, the U.S. aligns itself with countries that undermine respect for fundamental rights and the rule of law.”

During her confirmation hearing, Haspel insisted she would never allow torture at the CIA again, and she said she would be guided in the future by her own “moral compass.” But she resolutely avoided saying whether, at the time, she thought the secret detention and “enhanced interrogation” of suspected terrorists was moral.

That reticence prompted Sen. John McCain (R-Ariz.), who is in Arizona battling a rare and serious form of brain cancer, to release a statement urging his colleagues to oppose Haspel’s nomination. McCain’s forceful appeal, which carries the weight of his own experience enduring years of torture as a prisoner of war in Vietnam, appears to have swayed some Democrats who were on the fence about their vote and fellow Arizona Republican Jeff Flake to vote against Haspel.

But the opposition could not amass the numbers needed to block her confirmation — particularly after Senate Intelligence Committee Vice Chairman Mark R. Warner (D-Va.) declared this week that he would support her. A key to winning over skeptical Democrats was a letter Haspel wrote this week to Warner, at his urging, saying that “with the benefit of hindsight and my experience as a senior agency leader, the enhanced interrogation program is not one the CIA should have undertaken.”

But she stopped short of condemning the people “that made these hard calls,” and again cited “valuable intelligence collected” through the program — despite the findings of the committee’s report on torture, released in 2014, which concluded that the CIA’s methods were not a viable means of gaining information.

Most Democrats did not come around to Haspel’s side. The six who did are betting that Haspel, despite her intimate role in some of the CIA’s darkest operations, is the best person to ensure they’re not repeated.

“This was not an easy decision,” Sen. Heidi Heitkamp (D-N.D.), who is expected to face a tough Republican challenge in this year’s elections, said in a statement explaining her support.

“Ms. Haspel’s involvement in torture is deeply troubling as my friend and colleague, John McCain, so eloquently reminded us,” Heitkamp said. McCain had called on his colleagues to reject Haspel’s nomination, saying her “refusal to acknowledge torture’s immorality is disqualifying.”

Heitkamp said she was persuaded by her one-on-one meetings with Haspel. She “explained to me that the agency should not have employed such tactics in the past and has assured me that it will not do so in the future.”