KOT, Afghanistan—An Afghan spy agency is recruiting villagers for militias to hold back Islamic State fighters seeking to expand their foothold in this opium heartland in eastern Afghanistan.

The program, which one top official says the government hopes to roll out across the country and may later use against the Taliban, is President Ashraf Ghani’s riskiest attempt to defend rural villages—and a part of his much larger counterinsurgency strategy.

...