Ensemble density-functional theory for ab initio molecular dynamics of metals and finite-temperature insulators