diff --git a/changelogs/fragments/log_verbosity.yml b/changelogs/fragments/log_verbosity.yml new file mode 100644 index 00000000000..3e67aac8d94 --- /dev/null +++ b/changelogs/fragments/log_verbosity.yml @@ -0,0 +1,2 @@ +minor_changes: + - User can now set ansible.log to record higher verbosity than what is specified for display via new configuration item LOG_VERBOSITY. diff --git a/lib/ansible/config/base.yml b/lib/ansible/config/base.yml index 29136350e70..a6130b58bba 100644 --- a/lib/ansible/config/base.yml +++ b/lib/ansible/config/base.yml @@ -385,6 +385,15 @@ LOCALHOST_WARNING: - {key: localhost_warning, section: defaults} type: boolean version_added: "2.6" +LOG_VERBOSITY: + name: Default log verbosity + description: + - This will set log verbosity if higher than the normal display verbosity, otherwise it will match that. + env: [{name: ANSIBLE_LOG_VERBOSITY}] + ini: + - {key: log_verbosity, section: defaults} + type: int + version_added: "2.17" INVENTORY_UNPARSED_WARNING: name: Warning when no inventory files can be parsed, resulting in an implicit inventory with only localhost default: True diff --git a/lib/ansible/plugins/connection/ssh.py b/lib/ansible/plugins/connection/ssh.py index 9754397efa9..eca906e5be5 100644 --- a/lib/ansible/plugins/connection/ssh.py +++ b/lib/ansible/plugins/connection/ssh.py @@ -747,8 +747,8 @@ class Connection(ConnectionBase): self._add_args(b_command, b_args, u'disable batch mode for sshpass') b_command += [b'-b', b'-'] - if display.verbosity > 3: - b_command.append(b'-vvv') + if display.verbosity: + b_command.append(b'-' + (b'v' * display.verbosity)) # Next, we add ssh_args ssh_args = self.get_option('ssh_args') diff --git a/lib/ansible/utils/display.py b/lib/ansible/utils/display.py index 3f331ad8e73..7abacf023fe 100644 --- a/lib/ansible/utils/display.py +++ b/lib/ansible/utils/display.py @@ -283,6 +283,11 @@ class Display(metaclass=Singleton): self.columns = None self.verbosity = verbosity + if C.LOG_VERBOSITY is None: + self.log_verbosity = verbosity + else: + self.log_verbosity = max(verbosity, C.LOG_VERBOSITY) + # list of all deprecation messages to prevent duplicate display self._deprecations: dict[str, int] = {} self._warns: dict[str, int] = {} @@ -412,7 +417,13 @@ class Display(metaclass=Singleton): # raise if logger and not screen_only: - msg2 = nocolor.lstrip('\n') + self._log(nocolor, color) + + @proxy_display + def _log(self, msg: str, color: str | None = None, caplevel: int = 0): + + if self.log_verbosity > caplevel: + msg2 = msg.lstrip('\n') lvl = logging.INFO if color: @@ -458,6 +469,11 @@ class Display(metaclass=Singleton): self.display(msg, color=C.COLOR_VERBOSE, stderr=to_stderr) else: self.display("<%s> %s" % (host, msg), color=C.COLOR_VERBOSE, stderr=to_stderr) + elif self.log_verbosity > self.verbosity and self.log_verbosity > caplevel: + # we send to log if log was configured with higher verbosity + if host is not None: + msg = "<%s> %s" % (host, msg) + self._log(msg, C.COLOR_VERBOSE, caplevel) def get_deprecation_message( self,