The realignment is certain to raise questions about the militarization of intelligence and the extent to which the Pentagon and the C.I.A. are intertwined in Afghanistan, Iraq , Yemen and other trouble spots in the world.

Mr. Obama has been thinking about the changes since before Christmas, when he was handed a type-written list of candidates for the Pentagon post by his national security adviser, Thomas E. Donilon . Although Secretary of State Hillary Rodham Clinton was discussed in Washington as a candidate to succeed Mr. Gates, an administration official said “it was Leon the whole time.”

Photo

In giving the C.I.A. post to General Petraeus, who initially was distrusted by the White House, Mr. Obama is retaining a celebrated soldier with extensive knowledge of intelligence gathering in both Afghanistan and Iraq. His reputation was so formidable, officials said, that it was difficult to rotate him to another military post. He will retire from the Army to take this job, a senior official said.

General Petraeus, who commanded the 2007 surge in American forces in Iraq, has sought to earn the trust of the Obama White House by keeping a lower profile, even as Democrats fretted that he might have political ambitions. Last year, his work paid off when Mr. Obama turned to him to replace Gen. Stanley H. McChrystal as the commander in Afghanistan after a Rolling Stone magazine article that portrayed General McChrystal ’s staff as making disparaging remarks about the president’s national security team.

“The president went to creative lengths to keep Petraeus in the government,” a senior official said Wednesday.

A number of General Petraeus’s associates described him as extremely eager to take the appointment. “He welcomes the opportunity to ‘stay in the fight,’ as he put it,” said Michael O’Hanlon, a military expert at the Brookings Institution who talked to General Petraeus about the C.I.A. position in March.

Video

The military will face additional changes at the top, with the departure of the chairman of the Joint Chiefs of Staff , Adm. Mike Mullen , who, like Mr. Gates, was appointed by President George W. Bush , and whose term expires at the end of September. And Deputy Secretary of State James B. Steinberg has announced that he is leaving for an academic job — removing one of the key players in Mr. Obama’s efforts to manage China ’s rise.

Advertisement Continue reading the main story

But Mr. Gates’s role has been the most critical. He often allied on key issues with Mrs. Clinton — who has said that she intends to leave the government when this term ends — including persuading Mr. Obama to start the military buildup in Afghanistan in 2009. Together they won other battles, but they visibly split last month on the military intervention in Libya , which Mr. Gates warned against.

On Thursday, Mr. Obama will nominate Lt. Gen. John R. Allen, a Marine who is now deputy commander of the United States Central Command, as General Petraeus’s successor in Afghanistan. General Allen has close ties to Mr. Donilon, officials said. He also served in Iraq, where officials said he gained experience in reconciling with insurgents — a process being pursued with the Taliban in Afghanistan.

Newsletter Sign Up Continue reading the main story Please verify you're not a robot by clicking the box. Invalid email address. Please re-enter. You must select a newsletter to subscribe to. Sign Up You will receive emails containing news content , updates and promotions from The New York Times. You may opt-out at any time. You agree to receive occasional updates and special offers for The New York Times's products and services. Thank you for subscribing. An error has occurred. Please try again later. View all New York Times newsletters.

Mr. Obama will also name the veteran diplomat Ryan C. Crocker as the next ambassador to Afghanistan, officials said. That move would, at least briefly, reunite Mr. Crocker, a former ambassador to Baghdad, with General Petraeus, with whom he worked closely in Iraq during the Bush administration. Mr. Crocker served briefly in Kabul in 2002, reopening the embassy after the fall of the Taliban.

Mr. Crocker will replace Ambassador Karl W. Eikenberry , a retired lieutenant general and onetime commander in Afghanistan who had sometimes rocky relations with the President Hamid Karzai of Afghanistan. His departure completes a turnover of the American diplomatic roster in Afghanistan and Pakistan, which comes as the State Department is assuming a larger role from the Pentagon.