From c8360e7d1acdca07495045891d5811fd2e23f45a Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 28 Jul 2025 06:47:10 +0200 Subject: [PATCH] [PR #10445/1f8b5eea backport][stable-10] cronvar: Handle empty value string properly (#10495) cronvar: Handle empty value string properly (#10445) * Fix empty value issue in cronvar * Update changelog * Update plugins/modules/cronvar.py * Update changelogs/fragments/10445-cronvar-reject-empty-values.yml * Update tests/integration/targets/cronvar/tasks/main.yml * Update tests/integration/targets/cronvar/tasks/main.yml * Accept empty strings on cronvar * Update plugins/modules/cronvar.py * Update main.yml --------- (cherry picked from commit 1f8b5eea4cd80077d29b67b388c03ecdea61c2f1) Co-authored-by: Giorgos Drosos <56369797+gdrosos@users.noreply.github.com> Co-authored-by: Felix Fontein --- .../10445-cronvar-reject-empty-values.yml | 2 ++ plugins/modules/cronvar.py | 2 ++ .../integration/targets/cronvar/tasks/main.yml | 17 +++++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 changelogs/fragments/10445-cronvar-reject-empty-values.yml diff --git a/changelogs/fragments/10445-cronvar-reject-empty-values.yml b/changelogs/fragments/10445-cronvar-reject-empty-values.yml new file mode 100644 index 0000000000..1bf39619cc --- /dev/null +++ b/changelogs/fragments/10445-cronvar-reject-empty-values.yml @@ -0,0 +1,2 @@ +bugfixes: + - "cronvar - handle empty strings on ``value`` properly (https://github.com/ansible-collections/community.general/issues/10439, https://github.com/ansible-collections/community.general/pull/10445)." diff --git a/plugins/modules/cronvar.py b/plugins/modules/cronvar.py index 979c99bb91..5f7d02bfc3 100644 --- a/plugins/modules/cronvar.py +++ b/plugins/modules/cronvar.py @@ -396,6 +396,8 @@ def main(): old_value = cronvar.find_variable(name) if ensure_present: + if value == "" and old_value != "": + value = '""' if old_value is None: cronvar.add_variable(name, value, insertbefore, insertafter) changed = True diff --git a/tests/integration/targets/cronvar/tasks/main.yml b/tests/integration/targets/cronvar/tasks/main.yml index 7cf9602955..0d3ae30daf 100644 --- a/tests/integration/targets/cronvar/tasks/main.yml +++ b/tests/integration/targets/cronvar/tasks/main.yml @@ -123,6 +123,23 @@ - custom_varcheck2.stdout == '1' - custom_varcheck3.stdout == '0' + +- name: Add variable with empty string + community.general.cronvar: + name: EMPTY_VAR + value: "" + state: present + +- name: Assert empty var present + ansible.builtin.shell: crontab -l + register: result + changed_when: false + +- name: Assert line is quoted + ansible.builtin.assert: + that: >- + 'EMPTY_VAR=""' in result.stdout + - name: Attempt to add cron variable to non-existent parent directory cronvar: name: NOPARENT_VAR