Skip to content

Commit

Permalink
add a single target field to simplified playbook
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed May 12, 2023
1 parent fe6343e commit f35cd50
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ Here are the main differences between the two types of playbooks:
- The full playbook supports multiple tasks, while the simplified playbook only supports a single task. This means that the full playbook can execute multiple sets of commands, whereas the simplified playbook can only execute one set of commands.
- The full playbook supports various target types, such as `hosts`, `groups`, and `names`, while the simplified playbook only supports a single type, which is a list of names or host addresses. See the [Targets](#targets) section for more details.
- The simplified playbook does not support task-level `on_error`, `user`, and `ssh_key` fields, while the full playbook does. See the [Task details](#task-details) section for more information.
- The simplified playbook also has `target` field (in addition to `targets`) allows to set a single host/name only. This is useful when user want to run the playbook on a single host only. The full playbook does not have this field.

Both types of playbooks support the remaining fields and options.

Expand Down
11 changes: 9 additions & 2 deletions pkg/config/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type SimplePlayBook struct {
SSHKey string `yaml:"ssh_key" toml:"ssh_key"` // ssh key
Inventory string `yaml:"inventory" toml:"inventory"` // inventory file or url
Targets []string `yaml:"targets" toml:"targets"` // list of names
Target string `yaml:"target" toml:"target"` // a single target to run task on
Task []Cmd `yaml:"task" toml:"task"` // single task is a list of commands
}

Expand Down Expand Up @@ -219,7 +220,7 @@ func unmarshalPlaybookFile(fname string, data []byte, overrides *Overrides, res
}

errors := new(multierror.Error)
if err = unmarshal(data, res); err == nil {
if err = unmarshal(data, res); err == nil && len(res.Tasks) > 0 {
return nil // success, this is full PlayBook config
}
errors = multierror.Append(errors, err)
Expand All @@ -232,8 +233,14 @@ func unmarshalPlaybookFile(fname string, data []byte, overrides *Overrides, res
res.Tasks[0].Name = "default" // we have only one task, set it as default

hasInventory := simple.Inventory != "" || (overrides != nil && overrides.Inventory != "")

target := Target{}
for _, t := range simple.Targets {
targets := append([]string{}, simple.Targets...)
if simple.Target != "" {
targets = append(targets, simple.Target) // append target from simple playbook
}

for _, t := range targets {
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
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/playbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ func TestPlaybook_New(t *testing.T) {
{Host: "127.0.0.1", Port: 2222}}, c.Targets["default"].Hosts)
})

t.Run("simple playbook with a single target set", func(t *testing.T) {
c, err := New("testdata/simple-playbook-single-target.yml", nil, nil)
require.NoError(t, err)
require.Equal(t, 1, len(c.Tasks), "1 task")
assert.Equal(t, "default", c.Tasks[0].Name, "task name")
assert.Equal(t, 5, len(c.Tasks[0].Commands), "5 commands")

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

t.Run("playbook with secrets", func(t *testing.T) {
secProvider := &mocks.SecretProvider{
GetFunc: func(key string) (string, error) {
Expand Down
37 changes: 37 additions & 0 deletions pkg/config/testdata/simple-playbook-single-target.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
user: app
ssh_key: ~/.ssh/id_rsa

target: "127.0.0.1:2222"

task:
- name: wait
script: sleep 5

- name: copy configuration
copy: {"src": "/local/remark42.yml", "dst": "/srv/remark42.yml", "mkdir": true}

- name: some local command
options: {local: true}
script: |
ls -la /srv
du -hcs /srv
- name: git
before: "echo before git"
after: "echo after git"
onerror: "echo onerror git"
script: |
git clone https://example.com/remark42.git /srv || true # clone if doesn't exists, but don't fail if exists
cd /srv
git pull
- name: docker
options: {no_auto: true}
script: |
docker pull umputun/remark42:latest
docker stop remark42 || true
docker rm remark42 || true
docker run -d --name remark42 -p 8080:8080 umputun/remark42:latest
env:
FOO: bar
BAR: qux

0 comments on commit f35cd50

Please sign in to comment.