cronvar: Handle empty value string properly (#10445)

* Fix empty  value issue  in cronvar

* Update changelog

* Update plugins/modules/cronvar.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update changelogs/fragments/10445-cronvar-reject-empty-values.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update tests/integration/targets/cronvar/tasks/main.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update tests/integration/targets/cronvar/tasks/main.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Accept empty strings on cronvar

* Update plugins/modules/cronvar.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update main.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 1f8b5eea4c)
This commit is contained in:
Giorgos Drosos 2025-07-28 07:31:51 +03:00 committed by patchback[bot]
commit 666790d98c
3 changed files with 21 additions and 0 deletions

View file

@ -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)."

View file

@ -396,6 +396,8 @@ def main():
old_value = cronvar.find_variable(name) old_value = cronvar.find_variable(name)
if ensure_present: if ensure_present:
if value == "" and old_value != "":
value = '""'
if old_value is None: if old_value is None:
cronvar.add_variable(name, value, insertbefore, insertafter) cronvar.add_variable(name, value, insertbefore, insertafter)
changed = True changed = True

View file

@ -123,6 +123,23 @@
- custom_varcheck2.stdout == '1' - custom_varcheck2.stdout == '1'
- custom_varcheck3.stdout == '0' - 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 - name: Attempt to add cron variable to non-existent parent directory
cronvar: cronvar:
name: NOPARENT_VAR name: NOPARENT_VAR