mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
fix(anisbleserver): support complex yaml config in ansibleplaybook_instance
This commit is contained in:
@@ -138,26 +138,17 @@ func (ai *SAnsiblePlaybookInstance) runPlaybook(ctx context.Context, userCred mc
|
||||
return errors.Wrap(err, "unable to update ansibleplaybookinstance")
|
||||
}
|
||||
|
||||
convertJO := func(o jsonutils.JSONObject) map[string]interface{} {
|
||||
if o == nil {
|
||||
return map[string]interface{}{}
|
||||
}
|
||||
ret := make(map[string]interface{})
|
||||
o.Unmarshal(&ret)
|
||||
return ret
|
||||
}
|
||||
// merge configs
|
||||
params := make(map[string]interface{})
|
||||
for k, v := range convertJO(ar.DefaultParams) {
|
||||
params[k] = v
|
||||
}
|
||||
for k, v := range convertJO(ai.Params) {
|
||||
params[k] = v
|
||||
dp, params := ar.DefaultParams.(*jsonutils.JSONDict), ai.Params.(*jsonutils.JSONDict)
|
||||
for _, k := range dp.SortedKeys() {
|
||||
v, _ := dp.Get(k)
|
||||
params.Set(k, v)
|
||||
}
|
||||
ar.DefaultParams.(*jsonutils.JSONDict).SortedKeys()
|
||||
sess := ansiblev2.NewOfflineSession().
|
||||
Inventory(ai.Inventory).
|
||||
PrivateKey(privateKey).
|
||||
Configs(params).
|
||||
ConfigYaml(params.YAMLString()).
|
||||
PlaybookPath(ar.PlaybookPath).
|
||||
OutputWriter(&ansiblePlaybookOutputWriter{ai}).
|
||||
KeepTmpdir(options.Options.KeepTmpdir)
|
||||
|
||||
@@ -28,12 +28,12 @@ type OfflineSession struct {
|
||||
hostIp string
|
||||
hostName string
|
||||
configs map[string]interface{}
|
||||
configYaml string
|
||||
}
|
||||
|
||||
func NewOfflineSession() *OfflineSession {
|
||||
sess := &OfflineSession{
|
||||
PlaybookSessionBase: NewPlaybookSessionBase(),
|
||||
configs: map[string]interface{}{},
|
||||
}
|
||||
return sess
|
||||
}
|
||||
@@ -68,6 +68,11 @@ func (sess *OfflineSession) Configs(configs map[string]interface{}) *OfflineSess
|
||||
return sess
|
||||
}
|
||||
|
||||
func (sess *OfflineSession) ConfigYaml(yaml string) *OfflineSession {
|
||||
sess.configYaml = yaml
|
||||
return sess
|
||||
}
|
||||
|
||||
func (sess *OfflineSession) GetPlaybookPath() string {
|
||||
return sess.playbookPath
|
||||
}
|
||||
@@ -76,6 +81,10 @@ func (sess *OfflineSession) GetConfigs() map[string]interface{} {
|
||||
return sess.configs
|
||||
}
|
||||
|
||||
func (sess *OfflineSession) GetConfigYaml() string {
|
||||
return sess.configYaml
|
||||
}
|
||||
|
||||
func (sess *OfflineSession) Run(ctx context.Context) (err error) {
|
||||
return runnable{sess}.Run(ctx)
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ type IPlaybookSession interface {
|
||||
GetTimeout() int
|
||||
CheckAndSetRunning() bool
|
||||
SetStopped()
|
||||
GetConfigYaml() string
|
||||
}
|
||||
|
||||
type runnable struct {
|
||||
@@ -145,6 +146,13 @@ func (r runnable) Run(ctx context.Context) (err error) {
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to write config to file %s", config)
|
||||
}
|
||||
} else if r.GetConfigYaml() != "" {
|
||||
yml := r.GetConfigYaml()
|
||||
config = filepath.Join(tmpdir, "config")
|
||||
err = ioutil.WriteFile(config, []byte(yml), os.FileMode(0600))
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to write config to file %s", config)
|
||||
}
|
||||
}
|
||||
|
||||
// run modules one by one
|
||||
@@ -278,6 +286,10 @@ func (pb *PlaybookSessionBase) GetConfigs() map[string]interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pb *PlaybookSessionBase) GetConfigYaml() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (pb *PlaybookSessionBase) GetRequirements() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user