Skip to content

Instantly share code, notes, and snippets.

@griznog
Created August 29, 2023 13:52
Show Gist options
  • Save griznog/fab22104ed47ab66d512286609fa8caa to your computer and use it in GitHub Desktop.
Save griznog/fab22104ed47ab66d512286609fa8caa to your computer and use it in GitHub Desktop.
Example of using nodes.conf without any `struct` involved.
$ go mod init yamltesting
$ go mod tidy
$ go run main.go nodes.conf ifcfg.ww
# Node: cpu-a-1
TYPE=Ethernet
DEVICE=enp161s0f1np1
NAME=enp161s0f1np1
BOOTPROTO=static
DEVTIMEOUT=10
IPADDR=100.64.2.1
NETMASK=255.255.255.0
HWADDR=b8:ce:f6:a1:69:b7
ONBOOT=true
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
# Node: cpu-a-2
TYPE=Ethernet
DEVICE=enp161s0f1np1
NAME=enp161s0f1np1
BOOTPROTO=static
DEVTIMEOUT=10
IPADDR=100.64.2.2
NETMASK=255.255.255.0
HWADDR=b8:ce:f6:a1:67:6f
ONBOOT=true
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
...
{{- range $nodeName, $nodeDetails := .nodes -}}
{{- $netDevices := index $nodeDetails "network devices" -}}
{{- if $netDevices.clusternet -}}
# Node: {{$nodeName}}
TYPE={{ with index $netDevices.clusternet "type" -}}{{ . }}{{ else }}Ethernet{{ end }}
DEVICE={{ index $netDevices.clusternet "device" }}
NAME={{ index $netDevices.clusternet "device" }}
BOOTPROTO=static
DEVTIMEOUT=10
IPADDR={{ index $netDevices.clusternet "ipaddr" }}
NETMASK={{ with index $netDevices.clusternet "netmask" -}}{{ . }}{{ else }}255.255.255.0{{ end }}
{{- if index $netDevices.clusternet "gateway" -}}GATEWAY={{ index $netDevices.clusternet "gateway" }}{{ end }}
HWADDR={{ index $netDevices.clusternet "hwaddr" }}
ONBOOT={{ with index $netDevices.clusternet "onboot" -}}{{ . }}{{ else }}NO{{ end }}
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
{{- if index $netDevices.clusternet "ipaddr6" -}}IPV6ADDR="{{ index $netDevices.clusternet "ipaddr6" }}"{{ end }}
{{ end }}
{{ end }}
package main
import (
"io/ioutil"
"log"
"os"
"text/template"
yaml "gopkg.in/yaml.v2"
)
func main() {
if len(os.Args) != 3 {
log.Fatal("Usage: go run main.go <path_to_yaml> <path_to_template>")
}
yamlFilePath := os.Args[1]
templateFilePath := os.Args[2]
// Read the YAML file
yamlFileContent, err := ioutil.ReadFile(yamlFilePath)
if err != nil {
log.Fatalf("Failed to read the YAML file: %v", err)
}
// Read the template file
tmplFileContent, err := ioutil.ReadFile(templateFilePath)
if err != nil {
log.Fatalf("Failed to read the template file: %v", err)
}
// Parse the YAML file into a generic map
var data map[string]interface{}
if err := yaml.Unmarshal(yamlFileContent, &data); err != nil {
log.Fatalf("Failed to unmarshal the YAML content: %v", err)
}
tmpl, err := template.New("yamlTemplate").Parse(string(tmplFileContent))
if err != nil {
log.Fatalf("Failed to parse the template: %v", err)
}
err = tmpl.Execute(os.Stdout, data)
if err != nil {
log.Fatalf("Failed to execute the template: %v", err)
}
}
WW_INTERNAL: 0
nodes:
cpu-a-1:
cluster name: bruno
ipmi:
write: "false"
profiles:
- role-compute
- site-common-bruno
- site-networking-bruno
network devices:
clusternet:
onboot: "true"
device: enp161s0f1np1
hwaddr: b8:ce:f6:a1:69:b7
ipaddr: 100.64.2.1
tags:
infiniband:
type: infiniband
onboot: "true"
device: ib0
hwaddr: b8:ce:f6:a1:69:b6
ipaddr: 100.64.18.1
primary: "false"
tags:
ibclientid: ff:00:00:00:00:00:02:00:00:02:c9:00:b8:ce:f6:03:00:a1:69:b6
ibhwaddr: 00:00:10:49:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:a1:69:b6
ibportguid: b8:ce:f6:03:00:a1:69:b6
tags:
compute: clush
cpu-a-2:
cluster name: bruno
ipmi:
write: "false"
profiles:
- role-compute
- site-common-bruno
- site-networking-bruno
network devices:
clusternet:
onboot: "true"
device: enp161s0f1np1
hwaddr: b8:ce:f6:a1:67:6f
ipaddr: 100.64.2.2
tags:
infiniband:
type: infiniband
onboot: "true"
device: ib0
hwaddr: b8:ce:f6:a1:67:6e
ipaddr: 100.64.18.2
primary: "false"
tags:
ibclientid: ff:00:00:00:00:00:02:00:00:02:c9:00:b8:ce:f6:03:00:a1:67:6e
ibhwaddr: 00:00:10:48:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:a1:67:6e
ibportguid: b8:ce:f6:03:00:a1:67:6e
tags:
compute: clush
cpu-b-1:
cluster name: bruno
ipmi:
write: "false"
profiles:
- role-compute
- site-common-bruno
- site-networking-bruno
network devices:
clusternet:
onboot: "true"
device: enp161s0f1np1
hwaddr: b8:ce:f6:a1:67:67
ipaddr: 100.64.2.11
tags:
infiniband:
type: infiniband
onboot: "true"
device: ib0
hwaddr: b8:ce:f6:a1:67:66
ipaddr: 100.64.18.3
primary: "false"
tags:
ibclientid: ff:00:00:00:00:00:02:00:00:02:c9:00:b8:ce:f6:03:00:a1:67:66
ibhwaddr: 00:00:10:48:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:a1:67:66
ibportguid: b8:ce:f6:03:00:a1:67:66
cpu-b-2:
cluster name: bruno
ipmi:
write: "false"
profiles:
- role-compute
- site-common-bruno
- site-networking-bruno
network devices:
clusternet:
onboot: "true"
device: enp161s0f1np1
hwaddr: b8:ce:f6:a1:67:b7
ipaddr: 100.64.2.12
tags:
infiniband:
type: infiniband
onboot: "true"
device: ib0
hwaddr: b8:ce:f6:a1:67:b6
ipaddr: 100.64.18.4
primary: "false"
tags:
ibclientid: ff:00:00:00:00:00:02:00:00:02:c9:00:b8:ce:f6:03:00:a1:67:b6
ibhwaddr: 00:00:10:48:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:a1:67:b6
ibportguid: b8:ce:f6:03:00:a1:67:b6
cpu-b-3:
cluster name: bruno
ipmi:
write: "false"
profiles:
- role-compute
- site-common-bruno
- site-networking-bruno
network devices:
clusternet:
onboot: "true"
device: enp161s0f1np1
hwaddr: b8:ce:f6:a1:69:37
ipaddr: 100.64.2.13
tags:
infiniband:
type: infiniband
onboot: "true"
device: ib0
hwaddr: b8:ce:f6:a1:69:36
ipaddr: 100.64.18.5
primary: "false"
tags:
ibclientid: ff:00:00:00:00:00:02:00:00:02:c9:00:b8:ce:f6:03:00:a1:69:36
ibhwaddr: 00:00:10:48:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:a1:69:36
ibportguid: b8:ce:f6:03:00:a1:69:36
cpu-b-4:
cluster name: bruno
ipmi:
write: "false"
profiles:
- role-compute
- site-common-bruno
- site-networking-bruno
network devices:
clusternet:
onboot: "true"
device: enp161s0f1np1
hwaddr: b8:ce:f6:a1:67:47
ipaddr: 100.64.2.14
tags:
infiniband:
type: infiniband
onboot: "true"
device: ib0
hwaddr: b8:ce:f6:a1:67:46
ipaddr: 100.64.18.6
primary: "false"
tags:
ibclientid: ff:00:00:00:00:00:02:00:00:02:c9:00:b8:ce:f6:03:00:a1:67:46
ibhwaddr: 00:00:10:48:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:a1:67:46
ibportguid: b8:ce:f6:03:00:a1:67:46
cpu-b-5:
cluster name: bruno
ipmi:
write: "false"
profiles:
- role-compute
- site-common-bruno
- site-networking-bruno
network devices:
clusternet:
onboot: "true"
device: enp161s0f1np1
hwaddr: b8:ce:f6:a1:69:c7
ipaddr: 100.64.2.15
tags:
infiniband:
type: infiniband
onboot: "true"
device: ib0
hwaddr: b8:ce:f6:a1:69:c6
ipaddr: 100.64.18.7
primary: "false"
tags:
ibclientid: ff:00:00:00:00:00:02:00:00:02:c9:00:b8:ce:f6:03:00:a1:69:c6
ibhwaddr: 00:00:10:48:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:a1:69:c6
ibportguid: b8:ce:f6:03:00:a1:69:c6
cpu-b-6:
cluster name: bruno
ipmi:
write: "false"
profiles:
- role-compute
- site-common-bruno
- site-networking-bruno
network devices:
clusternet:
onboot: "true"
device: enp161s0f1np1
hwaddr: b8:ce:f6:a1:69:57
ipaddr: 100.64.2.16
tags:
infiniband:
type: infiniband
onboot: "true"
device: ib0
hwaddr: b8:ce:f6:a1:69:56
ipaddr: 100.64.18.8
primary: "false"
tags:
ibclientid: ff:00:00:00:00:00:02:00:00:02:c9:00:b8:ce:f6:03:00:a1:69:56
ibhwaddr: 00:00:10:48:fe:80:00:00:00:00:00:00:b8:ce:f6:03:00:a1:69:56
ibportguid: b8:ce:f6:03:00:a1:69:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment