The Messenger of Allah ﷺ said, "Hold fast to obedience in what you find pleasant and what you find disagreeable, in your hardship and in your ease, and even when others are preferred over you."
أخبرنا قتيبة قال حدثنا يعقوب عن أبي حازم عن أبي صالح
أن رسول الله ﷺ قال عليك بالطاعة في منشطك ومكرهك وعسرك ويسرك وأثرة عليك
Muslim narrated it in "Leadership," chapter on the obligation of obeying rulers in matters that do not involve disobedience [to Allah], and the prohibition of obeying them in disobedience, hadith 1836, from Qutayba, with this wording. It is also found in Kubra, hadith 7776.