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.
On 26 April 2017, Ansari announced his retirement from professional cricket with immediate effect.
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.
Ansari is of Pakistani descent through his father, Professor Khizar Humayun Ansari, who migrated from Lahore, while his mother, Professor Sarah Ansari, is English. Ansari went to Hampton School in the London Borough of Richmond, and came through the Surrey academy system, having represented the county before he turned nine and also representing England at U15 (a team he also captained), U17 and U19 level. He has an older brother, Akbar Ansari.
He was awarded a double-first degree by Cambridge University. In 2016 he completed a 40,000 word Masters dissertation on the 1960s American civil rights group, the Deacons for Defense and Justice, being awarded his Masters degree in history with Distinction at the end of the year by Royal Holloway, University of London.