我有以下攻略:
- 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消息,如下所示:
输出:
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?
我尝试了下面的搜索,但搜索失败:
- debug:
msg: "No Python Found on {{ inventory_hostname }}"
when: setupdata | join('') | regex_search('No python interpreters found for host')您能否建议如何捕获WARNING消息并在警告中搜索字符串?
发布于 2020-09-22 01:07:43
请注意,setupdata中有一个名为warnings的键,它是list[str],它是实际when的一个更好的候选者。您可以通过将when:更改为使用search test来测试该列表的成员,从学究的角度讲,这应该使用contains测试来应用,但是仅仅将列表提供给search似乎做了我所期望的事情,并且更容易阅读
- debug:
msg: "No Python Found on {{ inventory_hostname }}"
when: setupdata.warnings is search('No python interpreters')https://stackoverflow.com/questions/63983964
复制相似问题