首页 > 解决方案 > 如何使用ansible根据该行中特定字段的条件值从url逐行获取

问题描述

ip-address cpu ram .. 如果这些是不同的字段并且我有多行,如何仅获取那些 cpu 字段值大于 75 的行。

使用查找模块,我可以一一获取行,但是如何为字段应用条件。

- debug: msg="{{item}}"
  when: item > 30
  with_items: "{{ lookup('url', 'http://{{ inventory_hostname }}:9200/_cat/nodes?h=ram.percent', wantlist=True) }}"

这里所有的值都被打印出来了。未应用该条件。

标签: ansible

解决方案


因为你的测试是item在数值上是否大于30,但是item是一个字符串。

您至少希望尝试将 item 强制转换为int

debug: var=item
when: '(item|int) > 30'
with_items: # etc etc

推荐阅读