Seventeen other members of the group, busted in January 2014, received prison terms of three to 10 years for their activities, mostly in the north of the country, The Local reports.
The soldier, whose name is not mentioned, served in Melilla, one of two Spanish enclaves on Morocco's Mediterranean coast. After leaving the army he joined Al-Qaeda and fought in Afghanistan, Morocccan authorities said.
All 18 were charged with setting up a criminal group to carry out acts of terrorism, being part of a banned religious organization and of holding unauthorized public meetings.
According to official estimates, some 1,500 Moroccans are fighting or have fought in the ranks of jihadist organizations such as the Islamic State, and the government is working hard to break up the militant cells.
Dozens of people have been jailed in recent months on charges of terrorism.