Looking back, it seems clear there is a major behind-the-scenes battle between the military and the president over the president's use of the military within the US.
Esper is a weak man at the junction point of the conversation, who elevates keeping his job high on his list of priorities. But his reputation will actually be enhanced and not diminished by standing up for the rule of law. If he loses his job, so be it. The president's desires are beneath the constitution. His powers are granted to him from the people via the founding documents. If he breaches them, it is he who should be removed.
It is a shame that Mattis or someone of his standing isn't secretary. But that is the whole point, of course. Esper was picked because he was a weak man.