-
Notifications
You must be signed in to change notification settings - Fork 266
/
inventory.go
140 lines (118 loc) · 3.81 KB
/
inventory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2020 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package clab
import (
_ "embed"
"io"
"os"
"sort"
"text/template"
"github.com/srl-labs/containerlab/types"
)
//go:embed inventory_ansible.go.tpl
var ansibleInvT string
// AnsibleInventoryNode represents the data structure used to generate the ansible inventory file.
// It embeds the NodeConfig struct and adds the Username and Password fields extracted from
// the node registry.
type AnsibleInventoryNode struct {
*types.NodeConfig
}
// KindProps is the kind properties structure used to generate the ansible inventory file.
type KindProps struct {
Username string
Password string
NetworkOS string
AnsibleConn string
}
// AnsibleInventory represents the data structure used to generate the ansible inventory file.
type AnsibleInventory struct {
// clab node kinds
Kinds map[string]*KindProps
// clab nodes aggregated by their kind
Nodes map[string][]*AnsibleInventoryNode
// clab nodes aggregated by user-defined groups
Groups map[string][]*AnsibleInventoryNode
}
// GenerateInventories generate various inventory files and writes it to a lab location.
func (c *CLab) GenerateInventories() error {
ansibleInvFPath := c.TopoPaths.AnsibleInventoryFileAbsPath()
f, err := os.Create(ansibleInvFPath)
if err != nil {
return err
}
return c.generateAnsibleInventory(f)
}
// generateAnsibleInventory generates and writes ansible inventory file to w.
func (c *CLab) generateAnsibleInventory(w io.Writer) error {
inv := AnsibleInventory{
Kinds: make(map[string]*KindProps),
Nodes: make(map[string][]*AnsibleInventoryNode),
Groups: make(map[string][]*AnsibleInventoryNode),
}
for _, n := range c.Nodes {
ansibleNode := &AnsibleInventoryNode{
NodeConfig: n.Config(),
}
// add kindprops to the inventory struct
// the kindProps is passed as a ref and is populated
// down below
kindProps := &KindProps{}
inv.Kinds[n.Config().Kind] = kindProps
// add username and password to kind properties
// assumption is that all nodes of the same kind have the same credentials
nodeRegEntry := c.Reg.Kind(n.Config().Kind)
if nodeRegEntry != nil {
kindProps.Username = nodeRegEntry.Credentials().GetUsername()
kindProps.Password = nodeRegEntry.Credentials().GetPassword()
}
// add network_os to the node
kindProps.setNetworkOS(n.Config().Kind)
// add ansible_connection to the node
kindProps.setAnsibleConnection(n.Config().Kind)
inv.Nodes[n.Config().Kind] = append(inv.Nodes[n.Config().Kind], ansibleNode)
if n.Config().Labels["ansible-group"] != "" {
inv.Groups[n.Config().Labels["ansible-group"]] =
append(inv.Groups[n.Config().Labels["ansible-group"]], ansibleNode)
}
}
// sort nodes by name as they are not sorted originally
for _, nodes := range inv.Nodes {
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].ShortName < nodes[j].ShortName
})
}
// sort nodes-per-group by name as they are not sorted originally
for _, nodes := range inv.Groups {
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].ShortName < nodes[j].ShortName
})
}
t, err := template.New("ansible").Parse(ansibleInvT)
if err != nil {
return err
}
err = t.Execute(w, inv)
if err != nil {
return err
}
return err
}
// setNetworkOS sets the network_os variable for the kind.
func (n *KindProps) setNetworkOS(kind string) {
switch kind {
case "nokia_srlinux", "srl":
n.NetworkOS = "nokia.srlinux.srlinux"
case "nokia_sros", "vr-sros":
n.NetworkOS = "nokia.sros.md"
}
}
// setAnsibleConnection sets the ansible_connection variable for the kind.
func (n *KindProps) setAnsibleConnection(kind string) {
switch kind {
case "nokia_srlinux", "srl":
n.AnsibleConn = "ansible.netcommon.httpapi"
case "nokia_sros", "vr-sros":
n.AnsibleConn = "ansible.netcommon.network_cli"
}
}