Did the city of Ankh-Morpork have city walls? And a military force? How would it defend itself from invasions?
It has walls, and periodic invaders, none successful. Its near neighbours are all major trading partners with a massive vested interest in the continuing successful status quo, hence the City's national anthem; "We Can Rule You Wholesale"
When dragons belch and hippos flee
My thoughts, Ankh-Morpork, are of thee
Let others boast of martial dash
For we have boldly fought with cash
We own all your helmets, we own all your shoes.
We own all your generals - touch us and you’ll lose.
The Ultimate Discworld Companion
Ankh-Morpork does occasionally receive interference from bands of barbarians, but they don't seem to be particularly successful. The city has more esoteric defences than mere force of arms.
You can talk about ramps. You can talk about garlic. You can talk about France. Go on. But if you haven’t smelled Ankh-Morpork on a hot day you haven’t smelled anything.
The citizens are proud of it. They carry chairs outside to enjoy it on a really good day. They puff out their cheeks and slap their chests and comment cheerfully on its little distinctive nuances. They have even put up a statue to it, to commemorate the time when the troops of a rival state tried to invade by stealth one dark night and managed to get to the top of the walls before, to their horror, their nose plugs gave out. Rich merchants who have spent many years abroad sent back home for specially stoppered and sealed bottles of the stuff, which brings tears to their eyes.
The Light Fantastic
No enemies had ever taken Ankh-Morpork. Well, technically they had, quite often; the city welcomed free-spending barbarian invaders, but somehow the puzzled raiders always found, after a few days, that they didn’t own their own horses anymore, and within a couple of months they were just another minority group with its own graffiti and food shops.
To deter invasion, the city maintains a standing watch.
It was three in the morning. Sergeant Colon and Corporal Nobbs were reaching the conclusion that anyone who intended to invade Ankh-Morpork probably wasn’t going to do so now. And there was a good fire back in the watch house.
Also, the city walls are gated
They closed the gates. This was not the ominous activity that it might appear, since the keys had been lost long ago and latecomers usually threw gravel at the windows of the houses built on top of the wall until they found a friend to lift the bar. It was assumed that foreign invaders wouldn’t know which windows to throw gravel at.
Historically, the city had its own armies and fought its enemies with military force.
In recent times this had been quite difficult and her grandfather, for example, had to lead an expedition all the way to Howondaland in order to find some sworn enemies, although there was an adequate supply and a lot of swearing by the time he left. Earlier, of course, it had been a lot easier. Ramkin regiments had fought the city’s enemies all over the Sto Plains and had inflicted heroic casualties, quite often on people in the opposing armies
They used to have an army but seem to have by and large found it redundant following the Patrician's ascension to power:
Thousands of years ago the old empire had enforced the Pax Morporkia, which had said to the world: "Do not fight, or we will kill you." The Pax had arisen again, but this time it said: "If you fight, we’ll call in your mortgages. And incidentally, that’s my pike you’re pointing at me. I paid for that shield you’re holding. And take my helmet off when you speak to me, you horrible little debtor.
Even as recently as the events of 'Night Watch' they had a standing army with the military playing a similar role in the Glorious Revolution to that seen in the real life French Revolution (somewhat reluctant defenders of a broken old order who were unpractised in policing and tended to massacre civilians when provoked).
While Ankh Morpork is generally protected by its exuberant treatment of invading armies as a business opportunity, reckless war profiteering does sometimes put it in apparently hopeless military situations. In Jingo, the city has to be saved from invasion by a Vimes/Vetinari based deus ex machina as a result of a long running tendency to sell their best weapons to their potential enemies. A volunteer army is raised by Ankh Morpork to exploit a rising nationalist tide but it is noted that the well-armed Klatchian professional soldiers spend a lot of time sniggering at it.