下面的输出是日志消息,我需要检索日志计数,即 5。如何过滤日志计数并将其用作下一个任务的条件
How to modify this code to store value of count(5) in a variable use it as condition in next task
代码:
name: get system log count
connection: local
xml:
xmlstring: "{{log.content}}"
content: attribute
xpath: '/response/result/log/logs[@count]
register: log_count
name: print log count
debug:
msg: "{{log_count.matches}}"
Output:
ok: [10.46.255.101] => {
"msg": {
"actions": {
"namespaces": {},
"state": "present",
"xpath": "/response/result/log/logs[@count]"
},
"changed": false,
"count": 1,
"failed": false,
"matches": [
{
"logs": {
"count": "5",
"progress": "100"
}
}
]
我尝试使用 log_count.matches.count 但不起作用。
基本上我想从这个输出中获取计数值
<response status="success"><result>
<job>
<tenq>05:03:36</tenq>
<tdeq>05:03:36</tdeq>
<tlast>05:03:36</tlast>
<status>FIN</status>
<id>90</id>
</job>
<log>
<logs count="0" progress="100"/>
</log>
</result></response>
问:“我正在尝试从此输出中获取
count
值_”
基于 How to fetch value from API response with Ansible playbook?,一个最小的示例 playbook
---
- hosts: localhost
become: false
gather_facts: false
vars:
XML: "{{ lookup('file', 'response.xml') }}"
YML: "{{ XML | ansible.utils.from_xml }}"
tasks:
- debug:
msg: "{{ YML }}"
- name: Logs count
debug:
msg: "{{ YML.response.result.log.logs['@count'] }}"
将产生
的输出TASK [debug] *****************
ok: [localhost] =>
msg:
response:
'@status': success
result:
job:
id: '90'
status: FIN
tdeq: 05:03:36
tenq: 05:03:36
tlast: 05:03:36
log:
logs:
'@count': '0'
'@progress': '100'
TASK [Logs count] ************
ok: [localhost] =>
msg: '0'
问:“如何在下一个任务中使用它作为条件?”
示例中
- fail:
msg: No logs!
when: YML.response.result.log.logs['@count'] | int < 1
产生
的输出TASK [fail] ********************************
fatal: [localhost]: FAILED! => changed=false
msg: No logs!