Ansari made his Surrey debut against Sussex in September 2010 in a List A match, before making his first-class debut for Cambridge MCCU against Essex in April 2011. He had bowling figures of 5-33 in only his third first-class match, for Cambridge MCCU against his own county of Surrey, Kevin Pietersen being among his victims. He scored his maiden first-class century in 2014, for Surrey against Derbyshire. In Surrey's next first-class match, against Leicestershire, he had his first 5-wicket haul for the county with figures of 5-93.
International
Ansari made his One Day International debut for England against Ireland on 8 May 2015, in a game which was eventually abandoned due to rain. On 15 September 2015 he was announced as a member of the 16-man England squad visiting the United Arab Emirates the following month to play three Tests against Pakistan. However, on the same day that the announcement was made he dislocated his thumb when fielding against Lancashire and, as a result, was replaced in the England team by Samit Patel. Ansari was selected as a member of the England touring party to Bangladesh and India in 2016–17, and made his Test debut for England in the second Test against Bangladesh on 28 October. He took figures of 0-36 in the first innings, and made 13 with the bat. In Bangladesh's second innings he took 2-76, although he was out for a duck in England's second innings as they lost by 108 runs. Ansari played in the first two Tests against India. In the first Test, he made 32 in England's first innings total of 537. He took figures of 2-77 in India's first innings, before picking up another wicket in the second innings, although England were unable to force a victory. In the second Test he did not take a wicket, and was out for a duck in England's second innings as England lost the Test by 246 runs. A back injury prevented him from playing any further part in the tour.
Retirement
On 26 April 2017, at age 25, Ansari announced his retirement from professional cricket with immediate effect. He did not regard himself as a super-successful cricketer, such as Joe Root or Moeen Ali, so retirement did not feel like he was giving up something. He stated he had always maintained that cricket was just one part of his life, and that he had other ambitions that he wanted to fulfil. Ansari had a wide worldview and interests. Two years after retiring from cricket he said "On a day to day basis I think I’m a happier person."
Career after cricket
Since cricket, he has worked for "Just for Kids Law, a legal charity supporting young people with immigration, housing or school issues" and Inquest.