(1) Of the two disastrous post-9/11 wars, this was the "good war." It was morally and legally defensible; we had been attacked by people who were being harbored by the government of Afghanistan.
(2) Before launching this justified war, we didn't work hard to find an alternative path to justice. President Bush's demands on the Afghanistan government went well beyond turning over Osama Bin Laden and included the sort of subsequent infringements on Afghanistan's sovereignty that pretty much ensured a rebuff. Bush seemed to want war, and he got it.
(3) Not too far into the Afghanistan War, the "not good war"--the Iraq War, which wasn't morally or legally defensible--showed up and diverted attention and resources from Afghanistan. We'll never know whether continued focus on Afghanistan would have led to a happier outcome there, but it's hard to imagine it leading to a less happy one.
(4) Four years ago, candidate Obama decided to supplement his dovishness on Iraq with hawkishness on Afghanistan. He committed himself to gearing up the Afghanistan War, rather than promise the early withdrawal that seemed more in keeping with his actual beliefs.
I guess the lessons of these four steps to quagmire are, respectively, something like the following: (1) A "good" war isn't necessarily good in the sense of "good idea." (2) Never casually ignore pre-war exit ramps. (3) Avoid hubris--e.g. launching a new war when you're not done with the old one. (4) Never assume that presidential candidates, once in office, won't become prisoners of their hawkish posturing. If you want to review lessons 2,3, and 4 with Iran in mind, feel free; and if you consider bombing Iran morally justified, you can throw in lesson 1.
The overarching lesson is that once you launch even the best intentioned and most justified of wars, you become a hostage to fortune. Stuff happens--political stuff, geopolitical stuff--and suddenly events have spun out of control. The downsides of both the Afghanistan and Iraq wars turned out to be many, many times worse than their proponents said they'd be. (Though there was so little debate about the Afghanistan war that "proponents" is a misleadingly distinct category.) And if you look at the people now saying we should bomb Iran, they tend to be the people who were the most full-throated in their reassurances that the Afghanistan and Iraq wars would work out really nicely.