WASHINGTON — Defense Secretary Robert M. Gates, as he prepared to depart the government for the second time, said in an interview on Friday that the human costs of the wars in Iraq and Afghanistan had made him far more wary about unleashing the might of the American armed forces. 

“When I took this job, the United States was fighting two very difficult, very costly wars,” Mr. Gates said. “ And it has seemed to me: Let’s get this business wrapped up before we go looking for more opportunities.” 

“If we were about to be attacked or had been attacked or something happened that threatened a vital U.S. national interest, I would be the first in line to say, ‘Let’s go,’ ” Mr. Gates said. “ I will always be an advocate in terms of wars of necessity.  I am just much more cautious on wars of choice.” 

Most recently, he expressed major reservations about American intervention in Libya. 

In December 2006, Mr. Gates was brought on by President George W. Bush to fix Iraq, and he was kept on by President Obama to solve Afghanistan.  Even as a trained historian, he said, he has learned most clearly over the last four and a half years that wars “have taken longer and been more costly in lives and treasure” than anticipated.