Graeme Murty’s bid to win a piece of silverware which would greatly enhance his prospects of remaining as Rangers manager beyond the end of the season looked to be in serious jeopardy when a goalkeeping howler from Wes Foderingham gifted Alan Forrest - younger brother of Celtic winger James - an early opener for the League One side.
But on an afternoon when Somerset Park seemed to be bathed in sunshine one minute and then engulfed by blizzards the next, Rangers ultimately delivered an emphatic response to claim their place in the quarter-finals of a tournament they have not lifted since 2009.
Doubles from Alfredo Morelos and Josh Windass were complemented by fine strikes by Jason Cummings and Jamie Murphy to more than spare the blushes of their keeper.
Foderingham has been a generally consistent and often excellent performer for Rangers since joining the Ibrox club but he would have been mortified by the blunder which allowed Ayr to claim their 11th minute lead.
The hosts had started purposefully without really threatening to cut Rangers open before Forrest gratefully seized the gift presented to him when Foderingham failed to control a straightforward passback from David Bates on the edge of his own six yard box.
Even allowing for the uneven playing surface, it was a wretched piece of play from the keeper who was then unable to prevent Forrest stepping in to bundle the ball home from close range.
It took Rangers much longer than Murty would have hoped to find any sort of attacking momentum. Russell Martin’s header, which drifted just wide following a corner, was as close as they came to scoring in a disjointed opening spell.
Foderingham had clearly been rattled by his error and he looked uncertain again when he could only punch a Paddy Boyle cross weakly into the path of Declan McDaid who might have done better than blaze his shot over.
They were worrying times for Murty but his team gradually began to impose themselves on the tie and started to stretch the Ayr defence. Windass was unfortunate not to equalise in the 26th minute when his header from James Tavernier’s fine cross smacked back off the crossbar.
Greg Docherty, making his first starting appearance for Rangers, then created what appeared a certain goal for Morelos with a low drive across the face of the six yard box. But the striker perpetrated an extraordinary miss at the back post, managing to hook the ball over from almost right on the line.
The derisory abuse from the home fans in response to that sitter had barely subsided when Morelos made amends and drew Rangers level in the 31st minute. The frame of Jack Ruddy’s goal was rattled again, this time by a stinging left foot shot from Sean Goss. The ball rebounded into the path of Morelos who this time showed good reactions and composure to steer a first time shot high into the net.
Rangers finished the first half in the ascendancy. Tavernier should have done better than have a tame shot blocked after he had been played in by Morelos, then Windass saw a dangerous low cross cleared with no takers among his team-mates.
A heavy and sustained burst of snowfall during the interval saw the start of the second half delayed until referee Nick Walsh was satisfied the pitch markings had been sufficiently cleared by ground staff, two of whom caused much hilarity among the shivering supporters with their lengthy struggle to start up a hot-air blowing machine.
The conditions relented once more as a contest which was still very much in the balance resumed and there was a crucial moment of redemption from Foderingham as he prevented Ayr regaining the lead in the 54th minute, sprinting from his line to make a fine save from McDaid’s shot.
It proved to be a pivotal contribution as Rangers responded smartly to secure their passage to the quarter-finals with a burst of three goals in a seven-minute period which left a tiring Ayr side reeling.
Cummings put Rangers in front in the 66th minute with a stunning and instinctive finish, hooking a shot over his own shoulder and into the net from close range after Jamie Murphy and Goss had wrong-footed the Ayr defence with a short corner routine.
Three minutes later, Murphy and Declan John combined down the left with the full-back’s cross breaking to Windass who swept home a low shot from around 12 yards. With the shoulders of the Ayr players now visibly sagging, Rangers quickly made it 4-1 when Murphy’s cutback from the right teed up a simple tap-in for Morelos.
What had been a hugely testing afternoon for so long for Murty’s men was now just a canter to the finishing line. Windass grabbed his second of the tie when he headed home a fine cross from Goss before Murphy wrapped up an ultimately satisfying day for the visitors when he raced half the length of the pitch before guiding his first goal for Rangers low into the corner of the net.
AYR UNITED: Ruddy, Geggan (Faulds 83), Rose, Reid, Boyle; McDaid, Adams, Moffat, Forrest (McGuffie 75); Moore (Ferguson 70), Shankland. Subs not used: Hart, Murphy, McCowan.
RANGERS: Foderingham, Tavernier, Bates, Martin, John; Docherty, Goss, Windass (Halliday 84), Murphy; Cummings (Herrera 76), Morelos (Miller 76). Subs not used: Kelly, Cardoso, Candeias, Holt.