Skip to content

Commit

Permalink
fix tag to match on multiple hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed May 2, 2023
1 parent e4d2a04 commit 517387e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
9 changes: 6 additions & 3 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,18 +286,21 @@ func (p *PlayBook) TargetHosts(name string) ([]Destination, error) {
}

// try as a tag in inventory
res := []Destination{}
for _, h := range p.inventory.Groups["all"] {
if len(h.Tags) == 0 {
continue
}
for _, t := range h.Tags {
if strings.EqualFold(t, name) {
res := []Destination{h}
res[0].User = userOverride(h.User)
return res, nil
h.User = userOverride(h.User)
res = append(res, h)
}
}
}
if len(res) > 0 {
return res, nil
}

// try as single host name in inventory
for _, h := range p.inventory.Groups["all"] {
Expand Down
17 changes: 12 additions & 5 deletions app/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,11 @@ func TestTargetHosts(t *testing.T) {
Groups: map[string][]Destination{
"all": {
{Host: "host1.example.com", Port: 22, User: "user1"},
{Host: "host2.example.com", Port: 22, User: "defaultuser", Name: "host2"},
{Host: "host2.example.com", Port: 22, User: "defaultuser", Name: "host2", Tags: []string{"tag1"}},
{Host: "host3.example.com", Port: 22, User: "defaultuser", Name: "host3", Tags: []string{"tag1", "tag2"}},
},
"group1": {
{Host: "host2.example.com", Port: 2222, User: "defaultuser", Name: "host2"},
{Host: "host2.example.com", Port: 2222, User: "defaultuser", Name: "host2", Tags: []string{"tag1"}},
},
},
Hosts: []Destination{
Expand All @@ -359,19 +359,26 @@ func TestTargetHosts(t *testing.T) {
},
{
"target with groups", "target2", nil,
[]Destination{{Host: "host2.example.com", Port: 2222, User: "defaultuser", Name: "host2"}},
[]Destination{{Host: "host2.example.com", Port: 2222, User: "defaultuser", Name: "host2", Tags: []string{"tag1"}}},
false,
},
{
"target as group from inventory", "group1", nil,
[]Destination{{Host: "host2.example.com", Port: 2222, User: "defaultuser", Name: "host2"}},
[]Destination{{Host: "host2.example.com", Port: 2222, User: "defaultuser", Name: "host2", Tags: []string{"tag1"}}},
false,
},
{
"target as a tag from inventory", "tag2", nil,
[]Destination{{Host: "host3.example.com", Port: 22, User: "defaultuser", Name: "host3", Tags: []string{"tag1", "tag2"}}},
false,
},
{
"target as a tag matching multiple from inventory", "tag1", nil,
[]Destination{
{Name: "host2", Host: "host2.example.com", Port: 22, User: "defaultuser", Tags: []string{"tag1"}},
{Name: "host3", Host: "host3.example.com", Port: 22, User: "defaultuser", Tags: []string{"tag1", "tag2"}}},
false,
},
{
"target as single host by name from inventory", "host3", nil,
[]Destination{{Host: "host3.example.com", Port: 22, User: "defaultuser", Name: "host3", Tags: []string{"tag1", "tag2"}}},
Expand All @@ -389,7 +396,7 @@ func TestTargetHosts(t *testing.T) {
},
{
"target as single host address", "host2.example.com", nil,
[]Destination{{Host: "host2.example.com", Port: 22, User: "defaultuser", Name: "host2"}},
[]Destination{{Host: "host2.example.com", Port: 22, User: "defaultuser", Name: "host2", Tags: []string{"tag1"}}},
false,
},
{"invalid host:port format", "host5.example.com:invalid", nil, nil, true},
Expand Down

0 comments on commit 517387e

Please sign in to comment.