diff --git a/changelogs/fragments/pager.yml b/changelogs/fragments/pager.yml new file mode 100644 index 00000000000..26d288ad16b --- /dev/null +++ b/changelogs/fragments/pager.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - cli - handle empty value for PAGER (https://github.com/ansible/ansible/issues/86898). diff --git a/lib/ansible/cli/__init__.py b/lib/ansible/cli/__init__.py index 30af48c0976..385bac25187 100644 --- a/lib/ansible/cli/__init__.py +++ b/lib/ansible/cli/__init__.py @@ -514,17 +514,19 @@ class CLI(ABC): p = subprocess.Popen('less --version', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() if p.returncode == 0: - CLI.pager_pipe(text, 'less') + CLI.pager_pipe(text, pager='less') else: display.display(text, screen_only=True) @staticmethod - def pager_pipe(text): + def pager_pipe(text, pager=None): """ pipe text through a pager """ - if 'less' in CLI.PAGER: + pager_cmd = pager or CLI.PAGER + + if 'less' in pager_cmd: os.environ['LESS'] = CLI.LESS_OPTS try: - cmd = subprocess.Popen(CLI.PAGER, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout) + cmd = subprocess.Popen(pager_cmd, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout) cmd.communicate(input=to_bytes(text)) except (OSError, KeyboardInterrupt): pass diff --git a/test/integration/targets/ansible-doc/runme.sh b/test/integration/targets/ansible-doc/runme.sh index f47f9412581..b59e1b441db 100755 --- a/test/integration/targets/ansible-doc/runme.sh +++ b/test/integration/targets/ansible-doc/runme.sh @@ -59,6 +59,16 @@ current_out="$(ansible-doc --playbook-dir ./ testns.testcol.yolo --type test | s expected_out="$(sed '1 s/\(^> TEST testns\.testcol\.yolo\).*(.*)$/\1/' yolo-text.output)" test "$current_out" == "$expected_out" +# PAGER is set to less by default, so we need to test with other pagers +for pager in more cat; do + echo "testing with pager $pager" + PAGER=$pager ansible-doc --list testns.testcol --playbook-dir ./ 2>&1 | grep "${GREP_OPTS[@]}" -v "Invalid collection name" +done + +# set pager to empty string +echo "testing with pager empty" +PAGER="" ansible-doc --list testns.testcol --playbook-dir ./ 2>&1 | grep "${GREP_OPTS[@]}" -v "Invalid collection name" + # ensure we do work with valid collection name for list ansible-doc --list testns.testcol --playbook-dir ./ 2>&1 | grep -v "Invalid collection name"