As the nation reels from the murder of five Dallas cops and the deaths of two black men at the hands of police — including one in Baton Rouge — a gunman ambushed officers in the Louisiana city on Sunday, killing three, authorities said.

Gavin Eugene Long, a self-described member of the Nation of Islam and former Marine from Kansas City, Mo., launched the attack on his 29th birthday. Among the officers he killed was Montrell Jackson, a 32-year-old black man who had just become a father.

“For the second time in two weeks, police officers who put their lives on the line for ours every day were doing their job when they were killed in a cowardly and reprehensible assault,” President Obama said in an address hours later. “These attacks . . . have to stop.’’

The rampage began at 8:40 a.m., about a mile from Baton Rouge police headquarters, when officers responding to a 911 call spotted a masked Long dressed all in black and carrying an assault rifle.

Long — a black separatist who urged Americans to “fight back” against police in a series of rambling YouTube videos — was standing near a beauty-supply store on Airline Highway, where demonstrations have taken place in recent days.

Moments later, he opened fire in what Baton Rouge Mayor Kip Holden called an “ambush-style” attack.

“By 8:45, officers were down,” said Louisiana State Police Col. Mike Edmonson.

Cellphone footage captured at the scene shows the moment shots rang out as Long began firing indiscriminately after walking out of a convenience store and carwash.

In one clip, several shots are heard, immediately followed by a flurry of bangs as cops fire back.

Police said Long continued to shoot at cops as he ran down the road to the B-Quik gas station, before officers felled him.

Bystander Marquis Gibson was at the Hammond Aire Plaza mall when he heard shots and ran inside for cover.

“Officers were falling down and hiding,” he told NBC News.

Resident Mark Clement told the Times-Picayune he heard 10 to 12 gunshots.

Vehicles near the incident were seen peeling away for safety. Another surveillance video from a nearby shop showed a SWAT-team vehicle racing toward the scene.

The dead included two Baton Rouge police officers and one East Baton Rouge Parish sheriff’s deputy, according to Sheriff Sid Gautreaux.

Another three officers were injured, including one critically.

“Each of these individuals was married, each one of the individuals had a family,” Gaustreaux said, his voice choking with emotion. He added that a 41-year-old sheriff’s deputy was “fighting for his life as we speak.”

Officer Jackson was the first victim to be identified. The new father had been on the Baton Rouge Police Department for 10 years and “loved his job,” a friend, Darnell Murdock, told the Baton Rouge Advocate.

One of Jackson’s cousins, who goes by @RealDruBankz on Twitter, wrote that the killed cop had a newborn son.

“Dude just had a baby a couple months ago!!” he wrote. “Y’all took him from a family that needed him.”

Officer Matthew Gerald, 41, a former Marine and Black Hawk chief from the Army, also was killed. Gerald was a new recruit, having joined the Baton Rouge Police Department in March.

The third victim was identified as Sheriff’s Deputy Brad Garafola, who was once honored as a “deputy of the month.’’

Long was identified as a member of two homegrown hate organizations, with The Wall Street Journal reporting that he was affiliated with the New Freedom Group and MSNBC saying he was linked to the Sovereign Citizens.

Online, Long used the pseudonym “Cosmo Setepenra.” He touted himself as a “nutritionist, dietitian, personal trainer, author and spiritual advisor” who spent two years in Japan and did one tour in Iraq as a sergeant in the Marines before being honorably discharged in 2010.

Videos posted to YouTube portrayed him as an egotistical madman who referred to himself as an “Alpha Preneur” and “life coach” — including one chilling tirade about how he is simply affiliated with the “spirit of justice” and no one else.

In the wake of the controversial fatal shooting of black Baton Rouge resident Alton Sterling by cops on July 5, Long posted at least three clips about oppression and protests. He could also be seen cruising around the Louisiana city just days before his rampage.

“Let’s just go with the numbers, Let’s go with the history,” he says in an ominous clip posted July 10. “One hundred percent of revolutions . . . have been successful through fighting back, through bloodshed. Zero have been successful over simply protesting . . . We know what its gonna take. It’s only fighting back or money, that’s all they care about. Revenue or blood . . . Nothing else.”

In another YouTube video posted on July 8, Long urges his followers to not make assumptions about his affiliations and motives if he were ever to act out in the near future.

“If anything happens with me . . . Don’t affiliate me with nothing,” he says. “Yea, I was also a Nation of Islam member, [but] I’m not affiliated with it . . . Don’t try to say, ‘Oh, he was African, he was this and that.’ No! They’d try to put you with ISIS or some other terrorist group. No! I am affiliated with the spirit of justice.”

On July 7, Long retweeted a picture of a man shooting a pistol into the front seat of an NYPD squad car.

Early Sunday morning, Long tweeted: “Just bc you wake up every morning doesn’t mean that you’re living. And just bc you shed your physical body doesn’t mean that you’re dead.”

Describing Sunday’s shooting and the sniper assault in Dallas, Obama said, “These are attacks on public servants, on the rule of law, and on civilized society . . . These attacks are the work of cowards who speak for no one. They right no wrongs. They advance no causes.”

Louisiana Gov. John Bel Edwards called the attack “unjustifiable,” during a press conference with local and state law enforcement, saying there has to be a nationwide truce.

“The hatred just has to stop,” he said. “We have to do better. Our community is hurting.”

Baton Rouge has been shaken by protests since cops shot and killed Sterling, 37, outside a convenience store.

The fatal shooting, and that of Philando Castile by a cop in Falcon Heights, Minn., the next day, prompted the killings of five officers in Dallas on July 7.

Like their counterparts across the nation, Baton Rouge police have been on high alert. Last week, they arrested three people who they said plotted to kill cops with guns stolen from a pawn shop.

After Sunday’s bloodshed, Alton Sterling’s aunt, Veda Sterling, said she feared relations between cops and blacks would worsen even further.

“I can only imagine things will now get 100 times worse,” she said sadly.