我可以打印来自 Ansible 的警告消息吗?就像 Ansible 对内部警告所做的那样:
[WARNING]: Ignoring invalid attribute: xx
目标用途是警告,不是错误,因此它们不应结束剧本执行,但它们应清晰可见(采用标准 Ansible 紫色(。
用法示例:
- 我有一些最新版本的硬编码 URL。
- 该手册将下载最新的可用 URL。
- 如果 URL 不同,请打印警告。
- 由于源不受信任,因此下载的URL应仅用于比较,而不应直接使用。
下面是一个简单的过滤器插件,它将发出警告消息:
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
.
调用此筛选器的人为示例 playbook 可能如下所示:
---
- 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 |
|| ||
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
根据您的问题,如果您不信任提供的 URL,似乎您可能希望使剧本失败,但要回答有关生成的问题
[WARNING]: <supplied text>
消息 我知道的唯一方法是通过您自己的 ansible 模块或通过插件。
您的模块或插件将进行您描述的 URL 比较并发出适当的消息。
在 ansible 模块中,您创建的module
对象具有一个 warn
函数,您可以调用该函数,如下所示: module.warn('your text')
在插件中,您的代码将生成一个Display
对象,并且该对象将具有您调用的warning
函数,如下所示: display.warning('your text')
.
如果您的目标只是有一个可视的紫色警告消息,而不是您可以通过debug
模块生成的内容,这似乎需要做很多工作。
我已经设法做到了这一点,@daniel-ashton的回答对我帮助很大。
必须在 playbook 文件夹或角色文件夹中拥有/创建library
和filter_plugins
文件夹。
创建library/noop.py
:
#!/usr/bin/python
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.module_utils.basic import AnsibleModule
def run_module():
module_args = dict(
noop=dict(type='list', required=True)
)
result = dict(
changed=False
)
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()
并创建filter_plugins/warn_me.py
:
#!/usr/bin/python
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
设置一个名为 eg 的变量或事实。 bypass_folder_warn
,如: bypass_folder_warn: Folder check bypass enabled, make sure you know what you are doing
.
然后在剧本或角色任务文件中添加任务,如下所示:
- noop:
noop: '{{ bypass_folder_warn | warn_me()}}'
delegate_to: localhost
when: bypass_folder_check
结果是:
TASK [myrole : noop] ********************************************************
[WARNING]: Folder check bypass enabled, make sure you know what you are doing
ok: [localhost]
警告消息根据需要以标准 Ansible 紫色打印。