【问题标题】:Ansible: print warnings from playbookAnsible:从剧本打印警告
【发布时间】:2023-03-17 09:49:01
【问题描述】:

我能否像 Ansible 那样从 Ansible 打印警告以用于内部警告,例如:

 [WARNING]: Ignoring invalid attribute: xx

目标用途是警告,不是错误,因此它们不应结束 playbook 执行,但应清晰可见(标准 Ansible 紫色)。

示例用法:

  1. 我有一些最新版本的硬编码 URL。
  2. 剧本下载最新的可用 URL。
  3. 如果 URL 不同,则打印警告。
  4. 由于来源不可信,下载的网址仅供比较,不能直接使用。

【问题讨论】:

  • 您能否提供一些背景信息,说明您为什么/何时要发出警告以及为什么您想要警告而不是标准调试消息?您可以轻松地从 ansible 回调生成紫色 [WARNING] 消息,但创建回调只是为了产生一个 msg 似乎有点过头了,除非回调在生成消息的过程中做了一些有意义的事情。
  • 我添加了我的目标用途作为示例。
  • 我发现这个问题非常有效,目前接受的答案并不是真正的解决方案,因为它不允许用户创建警告。整个想法是在运行时发出警告,并能够在剧本结束时处理它们。
  • @sorin 我刚刚在 Ansible 上打开了一个功能请求:github.com/ansible/ansible/issues/67260

标签: ansible


【解决方案1】:

这是一个简单的过滤器插件,会发出警告消息:

from ansible.utils.display import Display


class FilterModule(object):
    def filters(self): return {'warn_me': self.warn_filter}

    def warn_filter(self, message, **kwargs):
        Display().warning(message)
        return message

将上述内容放入一个文件中,例如[playbook_dir]/filter_plugins/warn_me.py

调用此过滤器的人为示例剧本可能如下所示:

---
- name: Demonstrate warn_me filter plugin
  gather_facts: no
  hosts: all

  tasks:
    - meta: end_play 
      when: ('file XYZ cannot be processed' | warn_me())
      delegate_to: localhost
      run_once: yes

运行此剧本可能会产生以下输出:

$ ansible-playbook test_warnme.yml -l forwards
 __________________________________________ 
< PLAY [Demonstrate warn_me filter plugin] >
 ------------------------------------------ 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 [WARNING]: file XYZ cannot be processed

 ____________ 
< PLAY RECAP >
 ------------ 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


【讨论】:

  • 这个答案中的过滤器对我有用,尽管我使用set_fact 来执行并保存显示的警告。
【解决方案2】:

根据您的问题,如果您不信任提供的 URL,但要回答有关生成的问题,您可能希望让剧本失败

[WARNING]: &lt;supplied text&gt;

消息我知道的唯一方法是通过您自己的 ansible 模块或插件。

您的模块或插件将对您描述的 URL 进行比较并发出适当的消息。

在 ansible 模块中,您创建的 module 对象具有您调用的 warn 函数,如下所示: module.warn('your text')

在插件中,您的代码将生成一个Display 对象,并且该对象将具有您调用的warning 函数,如下所示: display.warning('your text').

如果您的目标只是获得一个可靠的紫色警告消息,而不是您可以通过 debug 模块生成的消息,那么这似乎需要做很多工作。

【讨论】:

    【解决方案3】:

    使用fail: + when: + ignore_errors: 可能会混淆警告。

    例如

    - name: Check hostname matches entry in ansible/hosts
      fail:
        msg: "Warning: Expected hostname to be '{{ inventory_hostname }}' but was '{{ ansible_facts.fqdn}}'"
      when: ansible_facts.fqdn != inventory_hostname
      ignore_errors: True
    

    警告显示为致命错误,然后是...skipping,它们的计数将在PLAY RECAP 中显示为ignored=N

    例如

    TASK [mytask: Check hostname matches entry in ansible/hosts] ***************
    fatal: [myhost]: FAILED! => {"changed": false, "msg": "Warning: Expected hostname to be 'myhost' but was 'myhost.my.example.com'"}
    ...ignoring
    
    PLAY RECAP *************************************************************************************
    myhost   ok=0    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    **ignored=1**
    

    信用:https://medium.com/opsops/how-to-print-warning-from-a-task-3286ebb39f40

    【讨论】:

      猜你喜欢
      • 2020-05-08
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      相关资源
      最近更新 更多