During the Events of “Revenge of the Sith”, Rex was with Ahsoka during “The Siege of Mandalore”. Rex and a few other clones found out about the inhibitor chips implanted in every Republic clone from the beginning of their birth stages. Before “Order 66" took effect, a clone trooper named “Fives”, who was a close friend of Rex and member of the 501st During “The Clone Wars” had proof of the clones being compromised for an unknown reason (Which was Palpatine’s plan to establish the Empire as Darth Sidous in secret and make sure that the Jedi Order would not be a problem in the foreseeable future).
 Watch “Star Wars The Clone Wars S6E1-S6E4" to see what occurs with Fives, This is one of the many best story arcs in the shows run. Fives started out in “Domino Squad” and by the time of “The Battle of Kamino”, got Rex’s interest in him as a member of the 501st. 

Back to Rex, since he had his inhibitor chip taken out, he was not affected by “Order 66". He helped Ahsoka escape Mandalore, and eventually ran into “Wollfe” and “Gregor”. If i am not mistaken, Rex tried to convince Commander Cody of the Discovery, but did not listen. Because in Cody’s mind, without the chips implanted, the clones loyal to the Republic would be no better than someone such as Jango Fett or the clones themselves would start turning against “The Republic”.