Merge pull request #10932 from rainzm/sshable/fix

Correctly execute the ansible playbook with the specified private key
This commit is contained in:
Zexi Li
2021-04-27 20:12:42 +08:00
committed by GitHub
2 changed files with 15 additions and 4 deletions

View File

@@ -25,6 +25,7 @@ import (
"yunion.io/x/pkg/errors"
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/ansibleserver/options"
"yunion.io/x/onecloud/pkg/apis"
api "yunion.io/x/onecloud/pkg/apis/ansible"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
@@ -186,10 +187,16 @@ func (apb *SAnsiblePlaybook) runPlaybook(ctx context.Context, userCred mcclient.
// init private key
pb := apb.Playbook.Copy()
if k, err := mcclient_modules.Sshkeypairs.FetchPrivateKey(ctx, userCred); err != nil {
return err
} else {
pb.PrivateKey = []byte(k)
if len(pb.PrivateKey) == 0 {
if k, err := mcclient_modules.Sshkeypairs.FetchPrivateKey(ctx, userCred); err != nil {
return err
} else {
pb.PrivateKey = []byte(k)
}
}
// init tmpdir clean policy
if options.Options.KeepTmpdir {
pb.CleanOnExit(false)
}
pb.OutputWriter(&ansiblePlaybookOutputWriter{apb})

View File

@@ -87,6 +87,10 @@ func (v *ValidatorAnsiblePlaybook) Validate(data *jsonutils.JSONDict) error {
}
}
}
// add LF for privateKey
if len(pb.PrivateKey) > 0 && pb.PrivateKey[len(pb.PrivateKey)-1] != 10 {
pb.PrivateKey = append(pb.PrivateKey, 10)
}
pbJson := jsonutils.Marshal(pb)
if serialized := pbJson.String(); len(serialized) > PlaybookMaxBytes {
return httperrors.NewBadRequestError("playbook too big, got %d bytes, exceeding %d",