Chaos and horrors followed President Trump’s needlessly abrupt withdrawal of US forces from northern Syria, but much of the mess was baked in long before Trump ever took office.

In other words, this president played the anti-ISIS hand that the last president had left him: An American exit was always in the cards, and all the players are falling into positions prompted by Obama-era US policy.

Thus the Syrian Kurds have allied with bloody Bashar al-Assad in the face of the Turkish invasion. This is less strange when you realize that the Obama administration chose the Kurds as its anti-ISIS ground troops in good part because they weren’t hostile to the Assad regime — whose rule has always been based on shielding Syria’s minorities against the country’s Sunni Arab majority.

Team Obama wanted to fight ISIS without crossing Assad’s chief allies, Iran and Russia — both key to that administration’s obsession with making the Iran nuclear deal work.

And, yes, Russia is also jumping in — even occupying bases America just abandoned. But Moscow re-entered the Mideast as a major player in 2013, when President Obama let it broker the deal to (supposedly) remove Assad’s chemical weapons after he crossed Obama’s infamous “red line.”

By the time Trump took office, Russia and Iran had already secured Assad’s hold on power — and established their own substantial military presence in Syria.

Obama had years earlier passed on the chance to seriously support Syria’s democratic rebels, writing them off as “farmers, dentists and folks who have never fought before.” Then again, he’s also the guy who, right before ISIS’s rapid expansion, laughed it off as “the JV team” compared to al Qaeda.

Nor is Turkish tyrant Recep Tayyip Erdogan’s eagerness to drive out the Kurds anything new. The Syrian Kurdish YPG is allied with the Turkish Kurds of the PKK — and Erdogan got Obama to limit what US arms went to the YPG even as it was taking on ISIS.

Of course, behind all the chaos is Obama’s fateful, foolish decision to completely withdraw US forces from Iraq in 2011 — which boosted Iran’s power there, infuriating the Iraqi Sunni population and so opening the door for ISIS’s rise.

Which should be a warning for the current president. As Rich Lowry noted in these pages: “Obama triumphantly exited Iraq, only to have to go back in when things spun out of control. If Trump wants to be done with Syria, it might not be done with him.”