Skip to content

Commit 6d12e83

Browse files
fix: Fix error faced when the ansible.operator-sdk/verbosity annotation for Ansible based-operators is 0 or less. (#2651)
Fix error faced when the `ansible.operator-sdk/verbosity` annotation for Ansible based-operators is 0 or less. Closes: #2627
1 parent a7c04ea commit 6d12e83

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- Fixed a regression in the `operator-sdk run` command that caused `--local` flags to be ignored ([#2478](https://github.com/operator-framework/operator-sdk/issues/2478))
4949
- Fix command `operator-sdk run --local` which was not working on Windows. ([#2481](https://github.com/operator-framework/operator-sdk/pull/2481))
5050
- Fix `ServiceMonitor` creation when the operator is cluster-scoped and the environment variable `WATCH_NAMESPACE` has a different value than the namespace where the operator is deployed. ([#2601](https://github.com/operator-framework/operator-sdk/pull/2601))
51+
- Fix error faced when the `ansible.operator-sdk/verbosity` annotation for Ansible based-operators is 0 or less. ([#2651](https://github.com/operator-framework/operator-sdk/pull/2651))
5152
- Fix missing error status when the error faced in the Ansible do not return an event status. ([#2661](https://github.com/operator-framework/operator-sdk/pull/2661))
5253

5354
## v0.15.2

pkg/ansible/runner/runner.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,42 @@ type Runner interface {
5757
GetFinalizer() (string, bool)
5858
}
5959

60+
// ansibleVerbosityString will return the string with the -v* levels
6061
func ansibleVerbosityString(verbosity int) string {
6162
if verbosity > 0 {
63+
// the default verbosity is 0
64+
// more info: https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-verbosity
6265
return fmt.Sprintf("-%v", strings.Repeat("v", verbosity))
6366
}
67+
// Return default verbosity
6468
return ""
6569
}
6670

6771
type cmdFuncType func(ident, inputDirPath string, maxArtifacts, verbosity int) *exec.Cmd
6872

6973
func playbookCmdFunc(path string) cmdFuncType {
7074
return func(ident, inputDirPath string, maxArtifacts, verbosity int) *exec.Cmd {
71-
return exec.Command("ansible-runner", ansibleVerbosityString(verbosity), "--rotate-artifacts",
75+
// check the verbosity since the exec.Command will fail if an arg as "" or " " be informed
76+
if verbosity > 0 {
77+
return exec.Command("ansible-runner", ansibleVerbosityString(verbosity), "--rotate-artifacts",
78+
fmt.Sprintf("%v", maxArtifacts), "-p", path, "-i", ident, "run", inputDirPath)
79+
}
80+
return exec.Command("ansible-runner", "--rotate-artifacts",
7281
fmt.Sprintf("%v", maxArtifacts), "-p", path, "-i", ident, "run", inputDirPath)
82+
7383
}
7484
}
7585

7686
func roleCmdFunc(path string) cmdFuncType {
7787
rolePath, roleName := filepath.Split(path)
7888
return func(ident, inputDirPath string, maxArtifacts, verbosity int) *exec.Cmd {
79-
return exec.Command("ansible-runner", ansibleVerbosityString(verbosity), "--rotate-artifacts",
89+
// check the verbosity since the exec.Command will fail if an arg as "" or " " be informed
90+
if verbosity > 0 {
91+
return exec.Command("ansible-runner", ansibleVerbosityString(verbosity), "--rotate-artifacts",
92+
fmt.Sprintf("%v", maxArtifacts), "--role", roleName, "--roles-path", rolePath, "--hosts",
93+
"localhost", "-i", ident, "run", inputDirPath)
94+
}
95+
return exec.Command("ansible-runner", "--rotate-artifacts",
8096
fmt.Sprintf("%v", maxArtifacts), "--role", roleName, "--roles-path", rolePath, "--hosts",
8197
"localhost", "-i", ident, "run", inputDirPath)
8298
}

pkg/ansible/runner/runner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func TestAnsibleVerbosityString(t *testing.T) {
219219
for _, tc := range testCases {
220220
gotString := ansibleVerbosityString(tc.verbosity)
221221
if tc.expectedString != gotString {
222-
t.Fatalf("Unexpected string %v expected %v", gotString, tc.expectedString)
222+
t.Fatalf("Unexpected string %v for expected %v from verbosity %v", gotString, tc.expectedString, tc.verbosity)
223223
}
224224
}
225225
}

test/ansible/deploy/crds/test.example.com_v1_inventorytest_cr.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ apiVersion: test.example.com/v1
22
kind: InventoryTest
33
metadata:
44
name: example-inventorytest
5+
annotations:
6+
"ansible.operator-sdk/verbosity": "3"
57
spec:
68
# Add fields here
79
size: 3

test/ansible/deploy/crds/test.example.com_v1alpha1_inventory_cr.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ kind: Inventory
33
metadata:
44
name: example-inventory
55
annotations:
6-
"ansible.operator-sdk/verbosity": "3"
6+
"ansible.operator-sdk/verbosity": "0"
77
spec:
88
# Add fields here
99
size: 3

0 commit comments

Comments
 (0)