【问题标题】:ansible: promoting warnings to errorsansible:提升对错误的警告
【发布时间】:2020-07-07 17:54:21
【问题描述】:

我有一些运行一些ansible-playbook 命令的持续集成检查。每个剧本可能会运行许多剧本,包括许多大型角色。

时不时,有人会引入一些更改,当ansible-playbook 运行时会导致警告,例如像这样:

[WARNING]: when statements should not include jinja2 templating delimiters
such as {{ }} or {% %}. Found: "{{ some_variable}}" not in
some_result.stdout

或:

[WARNING]: Consider using unarchive module rather than running tar

或一些弃用警告,例如:

[DEPRECATION WARNING]: ec2_facts is kept for backwards compatibility but usage 
is discouraged. The module documentation details page may explain more about 
this rationale.. This feature will be removed in a future release. Deprecation 
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

等等。有时当我们升级 ansible 版本时会弹出这些警告。不管它们为什么会发生,我真的很想通过某种方式让 ansible-playbook 命令在导致其中一个警告时大声失败,而不是安静地继续并让我的 CI 检查成功。有没有办法做到这一点?我目前使用的是 ansible 2.4.3。

我发现很多关于如何隐藏这些警告的讨论,但没有发现任何关于将它们提升为硬错误的信息。

【问题讨论】:

  • 非常好的问题,因为将警告视为错误可能在 CI 门上非常有用,因为它会迫使人们修复代码。
  • 考虑到这种情况发生在 CI 环境中,您可能需要考虑在 CI 过程中使用 docs.ansible.com/ansible-lint 来查找警告和其他代码异味。
  • 我打开了这个 -- github.com/ansible/ansible/issues/64175 -- 如果我有时间我会尝试处理一个拉取请求
  • 我已经使用了ansible-lint,但这在例如针对不存在的库存组运行剧本时无济于事。在这种情况下,ansible 会打印警告但没有错误,并且 CI 测试通过。我希望它在这种情况下失败。

标签: ansible warnings


【解决方案1】:

我也有同样的问题。我的解决方法是:

  • 使用 --check 和 'tee' 运行 playbook 到一个临时文件
  • 做一些 grep-magic 过滤掉 'WARNING]:'
  • 做一些 grep-sed-magic 来过滤掉不为零的结果(除了 ok/skipped)

我知道这并不理想,所以如果你有一个好的解决方案,请分享:-)

【讨论】:

    【解决方案2】:

    ansible.cfg里面有个选项any_errors_fatal,那放什么

    any_errors_fatal = True
    

    【讨论】:

    • 不是我想要的——any_errors_fatal 说它“如果有任何失败,会将所有主机标记为失败”。就我而言,我只有一个目标主机,但我希望将针对该主机的任何软警告提升为硬错误。
    【解决方案3】:

    要隐藏这样的[DEPRECATION WARNING],您可以 “通过在 ansible.cfg 中设置 deprecation_warnings=False 禁用。”

    但这可能并不理想,因为将来您无法看到可能发生的变化或贬值。

    如果你完全确定可以忽略这样的警告,你可以使用::

    args:
     warn: false
    

    或者按照警告消息中的建议修改您的代码。

    其他

    如果您想为任何警告引发错误,您可以注册结果并应用failed_when,如下例所示。

    - hosts: localhost
      gather_facts: false
      tasks:
         - name: Fail if there is any warnings
           shell: touch a.txt
           register: result
           failed_when: result.warnings is defined`
    

    【讨论】:

    • 谢谢,但我想做与隐藏警告相反的事情。我希望任何警告都会导致硬错误。
    • 我已经编辑了我的答案。为此,您可以注册结果并根据您的要求处理警告。
    • @Random_Automation 为了有用,必须将这些警告全局更改为错误。如果您已经知道哪个特定任务受到弃用警告,则不再需要该功能;您可以使用任何新构造来更新任务。
    【解决方案4】:

    为了什么?它们会干扰剧本的实施吗?很可能不是。只需修复 playbook 代码即可。

    据我所知,要删除这样的警告要么是不可能的,要么是非常困难的(无论如何,当我尝试时它对我不起作用):

    [WARNING]: when statements should not include jinja2 templating delimiters
    such as {{ }} or {% %}. Found: "{{ some_variable}}" not in
    some_result.stdout
    

    只需使用:

    some_variable not in some_result.stdout
    

    在“when”条件下,不需要设置 {{}} 来获取变量值

    我个人更喜欢通过“find”来搜索:

    some_result.stdout.find(some_variable) == -1
    

    此警告:

    [DEPRECATION WARNING]: ec2_facts is kept for backwards compatibility but usage 
    is discouraged. The module documentation details page may explain more about 
    this rationale.. This feature will be removed in a future release. Deprecation 
    warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
    

    可以通过设置从输出中移除:

    deprecation_warnings = false

    在 ansible.cfg 文件中

    此警告:

    [WARNING]: Consider using unarchive module rather than running tar
    

    如果您在“命令”中使用模块 ansible 而不是 tar,则易于删除:

    unarchive: src=file.tgz dest=/opt/dest
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 2013-03-08
      • 2013-04-24
      • 2017-10-03
      • 2011-08-22
      相关资源
      最近更新 更多