feat(ansible): support action shorthand key-value args

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 03:00:58 +00:00
parent c5712c696d
commit 6ef54d3e56
2 changed files with 57 additions and 3 deletions

View file

@ -562,15 +562,38 @@ func parseActionSpecString(raw string) (string, map[string]any) {
return "", nil
}
module := parts[0]
module := ""
start := 0
if kv := strings.SplitN(parts[0], "=", 2); len(kv) == 2 && kv[0] == "module" && kv[1] != "" {
module = kv[1]
start = 1
} else {
module = parts[0]
start = 1
}
if module == "" {
return "", nil
}
if len(parts) == 1 {
if start >= len(parts) {
return module, nil
}
args := make(map[string]any)
allKeyValues := true
for _, part := range parts[start:] {
key, value, ok := strings.Cut(part, "=")
if !ok || key == "" {
allKeyValues = false
break
}
args[key] = value
}
if allKeyValues && len(args) > 0 {
return module, args
}
sepIndex := strings.IndexFunc(raw, unicode.IsSpace)
if sepIndex < 0 || sepIndex >= len(raw)-1 {
return module, nil

View file

@ -320,6 +320,22 @@ action: command echo hello world
assert.Equal(t, "echo hello world", task.Args["_raw_params"])
}
func TestTypes_Task_UnmarshalYAML_Good_ActionAliasKeyValue(t *testing.T) {
input := `
name: Legacy action with args
action: module=copy src=/tmp/source dest=/tmp/dest mode=0644
`
var task Task
err := yaml.Unmarshal([]byte(input), &task)
require.NoError(t, err)
assert.Equal(t, "copy", task.Module)
require.NotNil(t, task.Args)
assert.Equal(t, "/tmp/source", task.Args["src"])
assert.Equal(t, "/tmp/dest", task.Args["dest"])
assert.Equal(t, "0644", task.Args["mode"])
}
func TestTypes_Task_UnmarshalYAML_Good_LocalAction(t *testing.T) {
input := `
name: Legacy local action
@ -335,11 +351,26 @@ local_action: shell echo local
assert.Equal(t, "echo local", task.Args["_raw_params"])
}
func TestTypes_Task_UnmarshalYAML_Good_LocalActionKeyValue(t *testing.T) {
input := `
name: Legacy local action with args
local_action: module=command chdir=/tmp
`
var task Task
err := yaml.Unmarshal([]byte(input), &task)
require.NoError(t, err)
assert.Equal(t, "command", task.Module)
assert.Equal(t, "localhost", task.Delegate)
require.NotNil(t, task.Args)
assert.Equal(t, "/tmp", task.Args["chdir"])
}
func TestTypes_Task_UnmarshalYAML_Good_WithNested(t *testing.T) {
input := `
name: Nested loop values
debug:
msg: "{{ item.0 }} {{ item.1 }}"
msg: "{{ item.0 }} {{ item.1 }}"
with_nested:
- - red
- blue