diff --git a/app/config/config.go b/app/config/config.go index 39a4a3f5..4840c47b 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -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"] { diff --git a/app/config/config_test.go b/app/config/config_test.go index c20e19a4..e0c27253 100644 --- a/app/config/config_test.go +++ b/app/config/config_test.go @@ -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{ @@ -359,12 +359,12 @@ 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, }, { @@ -372,6 +372,13 @@ func TestTargetHosts(t *testing.T) { []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"}}}, @@ -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},