From 2838aa364bcefeb5f2aacf0578aea431dace56da Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Fri, 27 Mar 2026 19:39:15 -0400 Subject: [PATCH] Review Comment and more tests Signed-off-by: Abhijeet Kasurde --- lib/ansible/modules/debconf.py | 27 ++++++------- .../targets/debconf/tasks/main.yml | 38 ++++++++++++++++++- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/lib/ansible/modules/debconf.py b/lib/ansible/modules/debconf.py index d827d38ee3b..228c77ca506 100644 --- a/lib/ansible/modules/debconf.py +++ b/lib/ansible/modules/debconf.py @@ -229,12 +229,22 @@ def main(): if vtype == 'seen': input_value = to_text(value).lower() - if input_value not in ['true', 'false']: - module.fail_json(msg=f"Invalid value provided for vtype 'seen': {input_value}") + valid_values = ['true', 'false'] + if input_value not in valid_values: + module.fail_json(msg=f"Invalid value provided for vtype {vtype}: {input_value}. Valid values are: {(', ').join(valid_values)}") value = input_value if vtype not in ('boolean', 'seen', 'multiselect'): - value = _check_type_str_no_conversion(value) + try: + value = _check_type_str_no_conversion(value) + except TypeError as exc: + module.fail_json(msg=f"Invalid value provided for vtype '{vtype}'", exception=exc) + + if vtype == 'multiselect' and isinstance(value, list): + try: + value = ", ".join(sorted(value)) + except TypeError as exc: + module.fail_json(msg=f"Invalid value provided for vtype '{vtype}': {to_native(exc)}") # if question doesn't exist, value cannot match if question not in prev: @@ -247,10 +257,6 @@ def main(): elif vtype == 'password': existing = get_password_value(module, pkg, question, vtype) elif vtype == 'multiselect' and isinstance(value, list): - try: - value = sorted(value) - except TypeError as exc: - module.fail_json(msg="Invalid value provided for 'multiselect': %s" % to_native(exc)) existing = sorted([i.strip() for i in existing.split(",")]) elif vtype == 'seen': # vtype seen is a special case where we only care if the question is seen or not @@ -265,18 +271,13 @@ def main(): if changed: if not module.check_mode: - if vtype == 'multiselect' and isinstance(value, list): - try: - value = ", ".join(value) - except TypeError as exc: - module.fail_json(msg="Invalid value provided for 'multiselect': %s" % to_native(exc)) rc, msg, e = set_selection(module, pkg, question, vtype, value, unseen) if rc: module.fail_json(msg=e) curr = {question: value} if question in prev: - prev = {question: prev[question]} + prev = {question: existing} else: prev[question] = '' diff --git a/test/integration/targets/debconf/tasks/main.yml b/test/integration/targets/debconf/tasks/main.yml index 1ee3d6cbd3f..0aff658a18a 100644 --- a/test/integration/targets/debconf/tasks/main.yml +++ b/test/integration/targets/debconf/tasks/main.yml @@ -294,7 +294,43 @@ assert: that: - not invalid_flag.changed - - '"is not a string and conversion is not allowed" in invalid_flag.msg' + - '"Invalid value provided for vtype" in invalid_flag.msg' + + - name: Set question for test + set_fact: + question7: ansible/test7 + + - name: Create a string question + debconf: + name: ansible + question: "{{ question7 }}" + vtype: string + value: "1" + diff: true + + - name: Mark as seen + debconf: + name: ansible + question: "{{ question7 }}" + vtype: seen + value: true + diff: true + + - name: Mark as seen false + debconf: + name: ansible + question: "{{ question7 }}" + vtype: seen + value: false + diff: true + register: result + + - name: Validate result + assert: + that: + - result.changed + - result.current[question7] == 'false' + - result.previous[question7] == 'true' always: - name: uninstall debconf-utils