From 9a07ab72b4638d0a7692c04a0775d22bd582d28a Mon Sep 17 00:00:00 2001 From: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:08:57 -0500 Subject: [PATCH] [2.16] expect - fix argument spec error with timeout=null (#82522) (#82608) * expect - fix argument spec error with timeout=null (#82522) * Fix using timeout=null to wait indefinitely * fix error message (cherry picked from commit da9edd7760526cb91cb0a3c28ce31c242a6e718e) * python2-ify --- .../fragments/fix-expect-indefinite-timeout.yml | 2 ++ lib/ansible/modules/expect.py | 12 ++++++++++-- test/integration/targets/expect/tasks/main.yml | 9 +++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/fix-expect-indefinite-timeout.yml diff --git a/changelogs/fragments/fix-expect-indefinite-timeout.yml b/changelogs/fragments/fix-expect-indefinite-timeout.yml new file mode 100644 index 00000000000..32bf6c274aa --- /dev/null +++ b/changelogs/fragments/fix-expect-indefinite-timeout.yml @@ -0,0 +1,2 @@ +bugfixes: + - expect - fix argument spec error using timeout=null (https://github.com/ansible/ansible/issues/80982). diff --git a/lib/ansible/modules/expect.py b/lib/ansible/modules/expect.py index 921aa70a447..8ff5cb4305b 100644 --- a/lib/ansible/modules/expect.py +++ b/lib/ansible/modules/expect.py @@ -43,7 +43,7 @@ options: responses. List functionality is new in 2.1. required: true timeout: - type: int + type: raw description: - Amount of time in seconds to wait for the expected strings. Use V(null) to disable timeout. @@ -122,6 +122,7 @@ except ImportError: from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.common.text.converters import to_bytes, to_native +from ansible.module_utils.common.validation import check_type_int def response_closure(module, question, responses): @@ -147,7 +148,7 @@ def main(): creates=dict(type='path'), removes=dict(type='path'), responses=dict(type='dict', required=True), - timeout=dict(type='int', default=30), + timeout=dict(type='raw', default=30), echo=dict(type='bool', default=False), ) ) @@ -162,6 +163,13 @@ def main(): removes = module.params['removes'] responses = module.params['responses'] timeout = module.params['timeout'] + if timeout is not None: + try: + timeout = check_type_int(timeout) + except TypeError as te: + module.fail_json( + msg="argument 'timeout' is of type {timeout_type} and we were unable to convert to int: {te}".format(timeout_type=type(timeout), te=te) + ) echo = module.params['echo'] events = dict() diff --git a/test/integration/targets/expect/tasks/main.yml b/test/integration/targets/expect/tasks/main.yml index 7bf18c5e5cf..2aef595717c 100644 --- a/test/integration/targets/expect/tasks/main.yml +++ b/test/integration/targets/expect/tasks/main.yml @@ -148,6 +148,15 @@ - "echo_result.stdout_lines[-2] == 'foobar'" - "echo_result.stdout_lines[-1] == 'bar'" +- name: test timeout is valid as null + expect: + command: "{{ansible_python_interpreter}} {{test_command_file}}" + responses: + foo: bar + echo: true + timeout: null # wait indefinitely + timeout: 2 # but shouldn't be waiting long + - name: test response list expect: command: "{{ansible_python_interpreter}} {{test_command_file}} foo foo"