WASHINGTON - America’s most prominent former military commander, David Petraeus, will plead guilty to illegally providing classified secrets to his mistress. The former CIA director and Iraq war general signed a plea deal and statement “that indicate he will plead guilty” to unauthorized removal and retention of classified material, the Justice Department said Tuesday.

The outcome marked a dramatic fall from grace for a four-star general who became the most revered commander of his generation for his role in the Iraq war.

But the plea deal will allow Petraeus to avoid a trial that would have shone a light on embarrassing details of his affair and his flouting of secrecy laws.

According to the legal documents, Petraeus acknowledged giving eight “black books” he kept as the commander in Afghanistan to his mistress and biographer, Paula Broadwell, as source material for her book about the general, “All In: The Education of General David Petraeus.”

The notebooks included his schedule, classified notes, the identities of covert officers, details about intelligence capabilities, code words and accounts of his meetings with President Barack Obama, according to court documents.

Passing the sensitive information to Broadwell and then keeping the notebooks at his home clearly violated his legal obligation to safeguard classified information, authorities said.

None of the classified information appeared in Broadwell’s book, which was published by Penguin in 2012.

Petraeus was given hero status in Washington for overseeing the troop “surge” in Iraq in 2006 and US leaders credited him for salvaging the troubled war effort. He later served as the top US commander in Afghanistan, where his tenure achieved mixed results.

Obama named him CIA director in 2011 but he resigned a year later after his affair with Broadwell was exposed.