An ensemble-based variational inversion scheme