diff --git a/changelogs/fragments/new_editor_pager_opts.yml b/changelogs/fragments/new_editor_pager_opts.yml new file mode 100644 index 00000000000..5e52f5b9551 --- /dev/null +++ b/changelogs/fragments/new_editor_pager_opts.yml @@ -0,0 +1,2 @@ +minor_changes: + - allow user to set ansible specific env vars for selecting pager and editor, but still fall back to commonly used defaults. diff --git a/lib/ansible/cli/__init__.py b/lib/ansible/cli/__init__.py index 15ab5fe17ea..d59f450944d 100644 --- a/lib/ansible/cli/__init__.py +++ b/lib/ansible/cli/__init__.py @@ -119,7 +119,7 @@ except ImportError: class CLI(ABC): ''' code behind bin/ansible* programs ''' - PAGER = 'less' + PAGER = C.config.get_config_value('PAGER') # -F (quit-if-one-screen) -R (allow raw ansi control chars) # -S (chop long lines) -X (disable termcap init and de-init) @@ -494,11 +494,11 @@ class CLI(ABC): # this is a much simpler form of what is in pydoc.py if not sys.stdout.isatty(): display.display(text, screen_only=True) - elif 'PAGER' in os.environ: + elif CLI.PAGER: if sys.platform == 'win32': display.display(text, screen_only=True) else: - CLI.pager_pipe(text, os.environ['PAGER']) + CLI.pager_pipe(text) else: p = subprocess.Popen('less --version', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() @@ -508,12 +508,12 @@ class CLI(ABC): display.display(text, screen_only=True) @staticmethod - def pager_pipe(text, cmd): + def pager_pipe(text): ''' pipe text through a pager ''' - if 'LESS' not in os.environ: + if 'less' in CLI.PAGER: os.environ['LESS'] = CLI.LESS_OPTS try: - cmd = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout) + cmd = subprocess.Popen(CLI.PAGER, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout) cmd.communicate(input=to_bytes(text)) except IOError: pass diff --git a/lib/ansible/cli/config.py b/lib/ansible/cli/config.py index 13c46f15641..165be732e8a 100755 --- a/lib/ansible/cli/config.py +++ b/lib/ansible/cli/config.py @@ -183,7 +183,7 @@ class ConfigCLI(CLI): # pylint: disable=unreachable try: - editor = shlex.split(os.environ.get('EDITOR', 'vi')) + editor = shlex.split(C.config.get_config_value('EDITOR')) editor.append(self.config_file) subprocess.call(editor) except Exception as e: diff --git a/lib/ansible/config/base.yml b/lib/ansible/config/base.yml index 92061402097..1901accf282 100644 --- a/lib/ansible/config/base.yml +++ b/lib/ansible/config/base.yml @@ -1003,6 +1003,19 @@ DEFAULT_STDOUT_CALLBACK: env: [{name: ANSIBLE_STDOUT_CALLBACK}] ini: - {key: stdout_callback, section: defaults} +EDITOR: + name: editor application touse + default: vi + descrioption: + - for the cases in which Ansible needs to return a file within an editor, this chooses the application to use + ini: + - section: defaults + key: editor + version_added: '2.15' + env: + - name: ANSIBLE_EDITOR + version_added: '2.15' + - name: EDITOR ENABLE_TASK_DEBUGGER: name: Whether to enable the task debugger default: False @@ -1739,6 +1752,19 @@ OLD_PLUGIN_CACHE_CLEARING: type: boolean default: False version_added: "2.8" +PAGER: + name: pager application to use + default: less + descrioption: + - for the cases in which Ansible needs to return output in pageable fasion, this chooses the application to use + ini: + - section: defaults + key: pager + version_added: '2.15' + env: + - name: ANSIBLE_PAGER + version_added: '2.15' + - name: PAGER PARAMIKO_HOST_KEY_AUTO_ADD: # TODO: move to plugin default: False diff --git a/lib/ansible/parsing/vault/__init__.py b/lib/ansible/parsing/vault/__init__.py index 8ac22d4ca9f..5a06c63625a 100644 --- a/lib/ansible/parsing/vault/__init__.py +++ b/lib/ansible/parsing/vault/__init__.py @@ -1123,7 +1123,7 @@ class VaultEditor: os.chown(dest, prev.st_uid, prev.st_gid) def _editor_shell_command(self, filename): - env_editor = os.environ.get('EDITOR', 'vi') + env_editor = C.config.get_config_value('EDITOR') editor = shlex.split(env_editor) editor.append(filename)