His actions breached the Official Secrets Act and the Terrorism Act.
Prosecutors said Khalife played a “cynical game” after he contacted a man linked to Iranian intelligence soon after joining the army in September 2018, before later telling MI6 he wanted to be a double agent.
He collected sensitive information that posed a real danger in the wrong hands, the court heard.
Khalife contacted a man linked to Iran’s Islamic Revolutionary Guard Corps (IRGC) on Facebook.
He built a relationship with Iranian contacts – at one point being sent to collect $2,000 (£1,500) left in a dog poo bag in a north London park.
Bethan David, head of the Crown Prosecution Service’s counter terrorism division, said that as a soldier, Khalife was “employed and entrusted to uphold and protect the national security of this country”.
“But, for purposes of his own, Daniel Khalife used his employment to undermine national security.”
She added that his information sharing “could have exposed military personnel to serious harm, or a risk to life, and prejudiced the safety and security of the United Kingdom”.