使用 CTRL+C 结束 ansible playbook 时隐藏 python 错误消息

使用 CTRL+C 结束 ansible playbook 时隐藏 python 错误消息

每当我使用 ctrl+c 终止 ansible playbook 时,它都会在 macOS 和 Ubuntu 上显示以下 python 错误消息:

^CProcess WorkerProcess-22:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.7/site-packages/ansible/executor/process/worker.py", line 118, in run
    self._final_q
  File "/usr/local/lib/python3.7/site-packages/ansible/executor/task_executor.py", line 140, in run
    res = self._execute()
  File "/usr/local/lib/python3.7/site-packages/ansible/executor/task_executor.py", line 612, in _execute
    result = self._handler.run(task_vars=variables)
  File "/usr/local/lib/python3.7/site-packages/ansible/plugins/action/shell.py", line 27, in run
    result = command_action.run(task_vars=task_vars)
  File "/usr/local/lib/python3.7/site-packages/ansible/plugins/action/command.py", line 24, in run
    results = merge_hash(results, self._execute_module(task_vars=task_vars, wrap_async=wrap_async))
  File "/usr/local/lib/python3.7/site-packages/ansible/plugins/action/__init__.py", line 840, in _execute_module
    res = self._low_level_execute_command(cmd, sudoable=sudoable, in_data=in_data)
  File "/usr/local/lib/python3.7/site-packages/ansible/plugins/action/__init__.py", line 959, in _low_level_execute_command
    rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
  File "/usr/local/lib/python3.7/site-packages/ansible/plugins/connection/local.py", line 133, in exec_command
    stdout, stderr = p.communicate(in_data)
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 939, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1681, in _communicate
    ready = selector.select(timeout)
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/selectors.py", line 415, in select
    fd_event_list = self._selector.poll(timeout)
KeyboardInterrupt

有什么办法可以隐藏它吗?我正在编写一个基于 CLI 的应用程序,用于 Mac、Ubuntu 和 Windows,我需要隐藏这个错误日志,但似乎找不到方法,无论是在 Ansible 中还是在 Python 中。

答案1

错误消息会打印到 stderr。只需使用标准重定向2>/dev/null即可抑制它。

username@hostname:~/playbooks$ ansible-playbook test.yml 2>/dev/null

PLAY [windows] *********************************************************************************************************

TASK [install all updates] *********************************************************************************************
^Cusername@hostname:~/playbooks$

相关内容