Mr. Mattis had repeatedly told friends and aides over recent months that he viewed his responsibility to protect the United States’ 1.3 million active-duty troops as worth the concessions necessary as defense secretary to a mercurial president.