Skip to content

Commit 7ad5d3d

Browse files
committedMay 1, 2023
fix ignoring filters on inventory overrides
1 parent f2a0cd4 commit 7ad5d3d

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ SimploTask supports the following command-line options:
4646
If not specified all the tasks will be executed.
4747
- `-d`, `--target=`: Specifies the target name to use for the task execution. The target should be defined in the playbook file and can represent remote hosts, inventory files, or inventory URLs. If not specified the `default` target will be used. User can pass a host name or IP instead of the target name for a quick override. Providing the `-d`, `--target` flag multiple times with different targets sets multiple destination targets or multiple hosts, e.g., `-d prod -d dev` or `-d example1.com -d example2.com`.
4848
- `-c`, `--concurrent=`: Sets the number of concurrent hosts to execute tasks. Defaults to `1`, which means hosts will be handled sequentially.
49-
- `ssh-timeout`: Sets the SSH timeout. Defaults to `30s`.
49+
- `timeout`: Sets the SSH timeout. Defaults to `30s`.
5050
- `-f`, `--filter=`: Filter destinations for the specified target. Providing the `-f` flag multiple times with different name, or hosts names or ips/fqdns allow multiple destination hosts from the selected target, e.g., `-f apollo -f h2.example2.com`
5151
- `--inventory-file=`: Specifies the inventory file to use for the task execution. Overrides the inventory file defined in the
5252
playbook file.

‎app/config/config.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,19 @@ func (p *PlayBook) TargetHosts(name string) ([]Destination, error) {
249249

250250
// check if we have overrides for inventory file, this is second priority
251251
if p.overrides != nil && p.overrides.InventoryFile != "" {
252-
return loadInventoryFile(p.overrides.InventoryFile, nil)
252+
res, err := loadInventoryFile(p.overrides.InventoryFile, nil)
253+
if err != nil {
254+
return nil, err
255+
}
256+
return p.filterHosts(res, p.overrides), nil
253257
}
254258
// check if we have overrides for inventory http, this is third priority
255259
if p.overrides != nil && p.overrides.InventoryURL != "" {
256-
return loadInventoryURL(p.overrides.InventoryURL, nil)
260+
res, err := loadInventoryURL(p.overrides.InventoryURL, nil)
261+
if err != nil {
262+
return nil, err
263+
}
264+
return p.filterHosts(res, p.overrides), nil
257265
}
258266

259267
// no overrides, check if we have target in config

‎app/config/config_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,17 @@ func TestPlayBook_TargetHostsOverrides(t *testing.T) {
469469
}, res)
470470
})
471471

472+
t.Run("override hosts with file, filtered", func(t *testing.T) {
473+
c, err := New("testdata/f1.yml", &Overrides{InventoryFile: "testdata/hosts-without-groups.yml", FilterHosts: []string{"h2", "h3"}})
474+
require.NoError(t, err)
475+
res, err := c.TargetHosts("blah")
476+
require.NoError(t, err)
477+
assert.Equal(t, []Destination{
478+
{Name: "h2", Host: "h2.example.com", Port: 2233, User: "umputun"},
479+
{Name: "h3", Host: "h3.example.com", Port: 22, User: "user1"},
480+
}, res)
481+
})
482+
472483
t.Run("override hosts with file not found", func(t *testing.T) {
473484
c, err := New("testdata/f1.yml", &Overrides{InventoryFile: "testdata/hosts_not_found"})
474485
require.NoError(t, err)
@@ -499,6 +510,24 @@ func TestPlayBook_TargetHostsOverrides(t *testing.T) {
499510
}, res)
500511
})
501512

513+
t.Run("override hosts with http, filtered", func(t *testing.T) {
514+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
515+
fh, err := os.Open("testdata/hosts-without-groups.yml")
516+
require.NoError(t, err)
517+
defer fh.Close()
518+
_, err = io.Copy(w, fh)
519+
require.NoError(t, err)
520+
}))
521+
defer ts.Close()
522+
c, err := New("testdata/f1.yml", &Overrides{InventoryURL: ts.URL, FilterHosts: []string{"h3", "h4.example.com"}})
523+
require.NoError(t, err)
524+
res, err := c.TargetHosts("blah")
525+
require.NoError(t, err)
526+
assert.Equal(t, []Destination{
527+
{Name: "h3", Host: "h3.example.com", Port: 22, User: "user1"},
528+
{Name: "h4", Host: "h4.example.com", Port: 22, User: "user2"},
529+
}, res)
530+
})
502531
t.Run("override hosts with http failed", func(t *testing.T) {
503532
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
504533
w.WriteHeader(http.StatusInternalServerError)

‎app/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type options struct {
2828
TaskName string `short:"t" long:"task" description:"task name"`
2929
Targets []string `short:"d" long:"target" description:"target name" default:"default"`
3030
Concurrent int `short:"c" long:"concurrent" description:"concurrent tasks" default:"1"`
31-
SSHTimeout time.Duration `long:"ssh-timeout" description:"ssh timeout" default:"30s"`
31+
SSHTimeout time.Duration `long:"timeout" description:"ssh timeout" default:"30s"`
3232

3333
// target overrides
3434
Filter []string `short:"f" long:"filter" description:"filter target hosts"`

0 commit comments

Comments
 (0)
Please sign in to comment.