Cardiff survived a late fightback by Welsh derby rivals Ospreys as they held on to win 24-21 and boost their United Rugby Championship play-off chances.