How to save command output to variable inside script?

I want do execute (via ssh) external command reading file write time, get its output and save to variable

  script:
- |      
  DT_BEFORE=$(ssh ${USER}@${SERVER} "stat -c %Y ${TARGET_FILE_CHECK} 2> /dev/null")
  echo $DT_BEFORE
  <skipped actions>
  DT_AFTER=$(ssh ${USER}@${SERVER} "stat -c %Y ${TARGET_FILE_CHECK} 2> /dev/null")
  if [ "$DT_AFTER" != "$DT_BEFORE" ]; then ssh ${USER}@${SERVER} "sudo systemctl restart ${TARGET_SERVICE}"; else echo "SKIP restart server - nothing changed"; fi

It doesnt work:

ERROR: Job failed: exit status 1

How to do that?