Skip to content

Commit

Permalink
mixing hosts and names caused duplicates
Browse files Browse the repository at this point in the history
detect host only if ip:port format
  • Loading branch information
umputun committed May 3, 2023
1 parent f0069de commit e675090
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 8 additions & 3 deletions app/config/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,15 @@ func unmarshalPlaybookFile(fname string, data []byte, res *PlayBook) (err error)
res.Inventory = simple.Inventory
res.Tasks = []Task{{Commands: simple.Task}} // simple playbook has just a list of commands as the task
res.Tasks[0].Name = "default" // we have only one task, set it as default
target := Target{Names: simple.Targets} // set as names to match inventory

target := Target{}
for _, t := range simple.Targets {
ip, port := splitIPAddress(t)
target.Hosts = append(target.Hosts, Destination{Host: ip, Port: port}) // also set as hosts
if strings.Contains(t, ":") {
ip, port := splitIPAddress(t)
target.Hosts = append(target.Hosts, Destination{Host: ip, Port: port}) // set as hosts in case of ip:port
} else {
target.Names = append(target.Names, t) // set as names in case of just name
}
}
res.Targets = map[string]Target{"default": target}
return nil
Expand Down
5 changes: 2 additions & 3 deletions app/config/playbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ func TestPlaybook_New(t *testing.T) {
assert.Equal(t, 5, len(c.Tasks[0].Commands), "5 commands")

assert.Equal(t, 1, len(c.Targets))
assert.Equal(t, []string{"name1", "name2", "127.0.0.1:2222"}, c.Targets["default"].Names)
assert.Equal(t, []Destination{{Host: "name1", Port: 22}, {Host: "name2", Port: 22}, {Host: "127.0.0.1", Port: 2222}},
c.Targets["default"].Hosts)
assert.Equal(t, []string{"name1", "name2"}, c.Targets["default"].Names)
assert.Equal(t, []Destination{{Host: "127.0.0.1", Port: 2222}}, c.Targets["default"].Hosts)
})
}

Expand Down

0 comments on commit e675090

Please sign in to comment.