Я пытаюсь создать следующий YAML, используя struct,
Ожидаемый результат:
all:
hosts:
children:
master:
hosts:
// This is dynamic data coming from the slice.
192.168.99.123:
192.168.99.125:
worker:
hosts:
192.168.99.123:
192.168.99.125:
etcd:
hosts:
192.168.99.123:
192.168.99.125:
vars:
ansible_user: vagrant
ansible_ssh_private_key_file: "~/.ssh/id_rsa"
Моя golang структура выглядит следующим образом:
type Inventory struct {
All *AnsibleInventory `json:"all"`
}
// AnsibleInventory defines ansible inventory file struct
type AnsibleInventory struct {
Hosts string `json:"hosts"`
Children *HostGroup `json:"children"`
Vars *Vars `json:"vars"`
}
type HostGroup struct {
Master *Hosts `json:"master"`
Worker *Hosts `json:"worker"`
Etcd *Hosts `json:"etcd"`
}
type Hosts struct {
Hosts map[string]string `json:"hosts,omitempty"`
}
type Vars struct {
User string `json:"ansible_user"`
SshPrivateKeyFile string `json:"ansible_ssh_private_key_file"`
}
Я инициализирую структуру следующим образом:
elementMap := make(map[string]string)
for _, ip := range p.MasterIPs {
elementMap[ip] = "" // I tried using slice this is definitely not expected
}
ansibleInventory := &Inventory{
&AnsibleInventory{Children: &HostGroup{
Master: &Hosts{
elementMap,
},
Worker: &Hosts{
elementMap,
},
Etcd: &Hosts{
elementMap,
},
}, Vars: &Vars{
User: p.Username,
SshPrivateKeyFile: p.PrivateKeyPath,
}}}
b, err := yaml.Marshal(ansibleInventory)
filename:= "/tmp/filename"
_, err = os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
err = ioutil.WriteFile(filename, b, 0644)
Полученный результат выглядит следующим образом:
all:
children:
etcd:
hosts:
// I want to avoid this empty quote
192.168.99.123: ""
192.168.99.125: ""
master:
hosts:
192.168.99.123: ""
192.168.99.125: ""
worker:
hosts:
192.168.99.123: ""
192.168.99.125: ""
// Same here aovid this empty quote
hosts: ""
vars:
ansible_ssh_private_key_file: ~/.ssh/id_rsa
ansible_user: vagrant