问题描述
我有以下剧本:
- 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 }}"
对于一些清单主机,当我按如下方式打印WARNING
变量时,收到以下setupdata
消息:
输出:
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
消息并在警告中搜索字符串吗?
解决方法
请注意,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')