WASHINGTON — CIA Deputy Director Michael Morell is stepping down and being replaced by White House lawyer Avril Haines, who will be the first woman to hold the post.

When President Barack Obama named a successor to former CIA Director David Petraeus in January, Morell was passed over in favor of the White House counterterrorism adviser, John Brennan. Morell had been acting director since Petraeus' resignation.

Morell, 54, announced his retirement Wednesday, saying he will leave his CIA post Aug. 9. The White House announced he has been appointed to the President's Intelligence Advisory Board, a group of mostly retired intelligence officers who advise the president on intelligence policy.

"While I have given everything I have to the Central Intelligence Agency and its vital mission for a third of a century, it is now time for me to give everything I have to my family," Morell said in a statement released Wednesday by the agency.

Morell is retiring after 33 years at the CIA, including two stints as acting director — during the last stint he managed the fallout inside the agency after Petraeus resigned over an extramarital affair. Morell ordered an internal investigation into his former boss' conduct that is ongoing.

"I was most looking forward to ... the opportunity to work side-by-side once again with Michael Morell," said Brennan, noting that they'd begun their careers at the CIA in 1980. "As much as I would selfishly like to keep Michael right where he is for as long as possible, he has decided to retire to spend more time with his family and to pursue other professional opportunities."

Haines, 43, has been a White House deputy assistant and deputy counsel for national security affairs since 2010. Before that, she was assistant legal adviser for treaty affairs at the State Department, according to a White House statement.

Haines had been nominated to serve as the State Department's legal adviser earlier this year, but Brennan chose her for the deputy post when Morell made clear that he was retiring.

Brennan said that Haines had participated in virtually every senior-level meeting at the president's National Security Council over the past two years and chairs the White House legal team that reviews the CIA's most sensitive programs.

"In every instance, Avril's command of substance, sense of mission, good judgment, and keen insights have been outstanding," Brennan said in his statement announcing the personnel changes.

Morell bore the brunt of defending before Congress the CIA's performance after the attack on the diplomatic outpost in Benghazi, Libya, last September, which killed the U.S. ambassador and three other Americans.

"Each time, his testimony has been thorough and forthright," said the top Democrat on the House Intelligence Committee, Rep. C.A. Dutch Ruppersberger of Maryland.

Morell did the final edit of the controversial "talking points" memo meant to describe to the public what happened in that attack. Emails exchanged by administration officials that week show Morell deleted references to the CIA warning the State Department of previous militant attacks in Benghazi.

U.N. Ambassador Susan Rice used those notes on Sunday talk shows, triggering a controversy in which House Republicans accuse the administration of covering up any connections to terror groups in the attack during the presidential campaign.

Rice was named national security adviser last week, replacing long-serving staffer Tom Donilon as part of the Obama administration's second-term turnover of its national security team.

Morell also put himself on a political collision course with the White House and top Democrats on the Senate Intelligence Committee by publicly defending the results of harsh CIA interrogation techniques like waterboarding while not defending the techniques themselves. Morell had said in a statement to CIA employees that while the film "Zero Dark Thirty" was wrong to depict harsh techniques as key to finding Osama bin Laden, those interrogations did produce some useful intelligence.

"Some came from detainees subjected to enhanced techniques, but there were many other sources as well," Morell said.

A Senate Intelligence Committee investigation into the CIA's detainee interrogation program completed last December concluded that interrogation methods such as waterboarding, which simulates drowning, produced no useful intelligence. And Morell's new boss, Brennan, told lawmakers during his confirmation hearing that after reading a summary of that report, he is now not sure those techniques produced any useful information.

Brennan further called the use of such techniques during the George W. Bush administration "something that is reprehensible and should never be done again."

Morell's defense of the program won him many backers at agency headquarters at Langley, Va.

"Morell is beloved in the building," former House Intelligence Committee member Jane Harman said Wednesday. "But it will be nice to have a woman in the job."