The IRS rules indicate that you have to live in it 2 of the last 5 years to get the capital gains exclusion, it doesn't have to be 2 consecutive years:
If you owned the home and used it as your residence for at least 24
months of the previous 5 years, you meet the residence requirement.
The 24 months of residence can fall anywhere within the 5-year period,
and it doesn'tt have to be a single block of time. All that is
required is a total of 24 months (730 days) of residence during the
5-year period. Unlike the ownership requirement, each spouse must meet
the residence requirement individually for a married couple filing
jointly to get the full exclusion.
There are other requirements to qualify for the exclusion, listed in the linked document.
In addition to capital gains tax, you'll have depreciation recapture to pay, that one is unavoidable if selling and not rolling it into another rental.