首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将可能的警告消息捕获到变量

将可能的警告消息捕获到变量
EN

Stack Overflow用户
提问于 2020-09-21 05:45:50
回答 1查看 119关注 0票数 1

我有以下攻略:

代码语言:javascript
复制
- name: Play 1.5 - Check Python on each target
  hosts: "{{ location }}"
  user: "{{ USER }}"
  ignore_unreachable: yes
  ignore_errors: yes
  gather_facts: false
  tasks:

   - setup:
       gather_subset:
         - network
     register: setupdata

   - debug:
       msg: "SETUP: {{ setupdata }}"

对于一些清单主机,我在打印setupdata变量时得到以下WARNING消息,如下所示:

输出:

代码语言:javascript
复制
ok: [10.9.10.16] => {
    "msg": "SETUP: {'warnings': [u\"No python interpreters found for host 10.9.10.16 (tried ['/usr/bin/python', 'python3.7', 'python3.6', 'python3.5', 'python2.7', 'python2.6', '/usr/libexec/platform-python', '/usr/bin/python3', 'python'])\"], 'module_stderr': u'This system is for the use of authorized users only. Individuals using this computer system without authority, or in excess of their authority, are subject to having all of their activities on this system monitored and recorded by system personnel. In the course of monitoring individuals improperly using this system, or in the course of system maintenance, the activities of authorized users may also be monitored. Anyone using this system expressly consents to such monitoring and is advised that if such such monitoring reveals possible evidence of criminal activity, system personnel may provide the evidence of such monitoring to the law enforcement officials\\n/bin/sh: /usr/bin/python:  not found\\n', 'changed': False, 'module_stdout': u'', 'failed': True, 'rc': 127, 'msg': u'MODULE FAILURE\\nSee stdout/stderr for the exact error', 'ansible_facts': {u'discovered_interpreter_python': u'/usr/bin/python'}}"
}

是否可以在上面的输出中搜索字符串No python interpreters found for host

我尝试了下面的搜索,但搜索失败:

代码语言:javascript
复制
   - debug:
       msg: "No Python Found on {{ inventory_hostname }}"
     when: setupdata | join('') | regex_search('No python interpreters found for host')

您能否建议如何捕获WARNING消息并在警告中搜索字符串?

EN

回答 1

Stack Overflow用户

发布于 2020-09-22 01:07:43

请注意,setupdata中有一个名为warnings的键,它是list[str],它是实际when的一个更好的候选者。您可以通过将when:更改为使用search test来测试该列表的成员,从学究的角度讲,这应该使用contains测试来应用,但是仅仅将列表提供给search似乎做了我所期望的事情,并且更容易阅读

代码语言:javascript
复制
- debug:
    msg: "No Python Found on {{ inventory_hostname }}"
  when: setupdata.warnings is search('No python interpreters')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63983964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档