KABUL—Afghanistan is rolling out an ambitious plan to spy on its own soldiers, the most serious attempt so far to halt a string of attacks by Afghan troops on their Western comrades-in-arms, according to Afghan and American military leaders.

As part of the effort, agents of the National Directorate of Security, the country's spy agency, will be deployed to army units across the country to monitor Afghan soldiers at every step, from recruitment and training to deployment and home leave, these people said.

The...