Skip to content

Commit

Permalink
[2.16] expect - fix argument spec error with timeout=null (#82522) (#…
Browse files Browse the repository at this point in the history
…82608)

* expect - fix argument spec error with timeout=null (#82522)

* Fix using timeout=null to wait indefinitely

* fix error message

(cherry picked from commit da9edd7)

* python2-ify
  • Loading branch information
s-hertel committed Feb 14, 2024
1 parent 3e3c29b commit 9a07ab7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/fix-expect-indefinite-timeout.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- expect - fix argument spec error using timeout=null (https://github.com/ansible/ansible/issues/80982).
12 changes: 10 additions & 2 deletions lib/ansible/modules/expect.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
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.
Expand Down Expand Up @@ -122,6 +122,7 @@

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):
Expand All @@ -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),
)
)
Expand All @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions test/integration/targets/expect/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 9a07ab7

Please sign in to comment.