You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ansible的register结果中查找指定Elastic ISM policy_id

问题描述

我通过API获取Elasticsearch的ISM策略,结果已保存到index_list变量中。需要仅当指定策略ID(示例为what)存在于返回结果里时,才执行后续任务,但当前的判断逻辑无法生效,需遍历返回结果的特定字段来实现判断。

API调用代码

- name: 获取ISM_retention策略
  uri:
    url: http://elastic:9200/_opendistro/_ism/policies
    method: GET
    timeout: 180
    body_format: json
  register: index_list

尝试的debug条件判断

尝试判断返回结果中是否存在policy_id: what,但逻辑不生效:

- debug:
    msg: hello
  when: "'what' in index_list"

register输出内容(已标记目标policy_id)

{
    u'status': 200,
    u'content_length': u'1919',
    u'cookies': {},
    u'url': u'http://elastic:9200/_opendistro/_ism/policies',
    u'changed': False,
    u'elapsed': 0,
    u'failed': False,
    u'json': {
        u'total_policies': 3,
        u'policies': [
            {
                u'policy': {
                    u'default_state': u'hot',
                    u'description': u'policy for delete index',
                    u'last_updated_time': 1667984798760,
                    u'error_notification': None,
                    u'states': [
                        {
                            u'transitions': [{u'conditions': {u'min_index_age': u'1d'}, u'state_name': u'delete'}],
                            u'name': u'hot',
                            u'actions': [{u'retry': {u'count': 3, u'delay': u'1m', u'backoff': u'exponential'}, u'open': {}}]
                        },
                        {
                            u'transitions': [],
                            u'name': u'delete',
                            u'actions': [{u'retry': {u'count': 3, u'delay': u'1m', u'backoff': u'exponential'}, u'delete': {}}]
                        }
                    ],
                    u'ism_template': [{u'index_patterns': [u'audit-*'], u'priority': 100, u'last_updated_time': 1667984798760}],
                    u'schema_version': 15,
                    u'policy_id': u'policy_1'
                },
                u'_id': u'policy_1',
                u'_seq_no': 104149,
                u'_primary_term': 1
            },
            {
                u'policy': {
                    u'default_state': u'hot',
                    u'description': u'kuku index retenation flow',
                    u'last_updated_time': 1668061803458,
                    u'error_notification': None,
                    u'states': [
                        {
                            u'transitions': [{u'conditions': {u'min_index_age': u'1d'}, u'state_name': u'delete'}],
                            u'name': u'hot',
                            u'actions': [{u'retry': {u'count': 3, u'delay': u'1m', u'backoff': u'exponential'}, u'open': {}}]
                        },
                        {
                            u'transitions': [],
                            u'name': u'delete',
                            u'actions': [{u'retry': {u'count': 3, u'delay': u'1m', u'backoff': u'exponential'}, u'delete': {}}]
                        }
                    ],
                    u'ism_template': [{u'index_patterns': [u'kuku-*'], u'priority': 100, u'last_updated_time': 1668061803458}],
                    u'schema_version': 15,
                    u'policy_id': u'policy_kuku'
                },
                u'_id': u'policy_kuku',
                u'_seq_no': 143284,
                u'_primary_term': 1
            },
            {
                u'policy': {
                    u'default_state': u'hot',
                    u'description': u'what index retenation flow',
                    u'last_updated_time': 1668074528411,
                    u'error_notification': None,
                    u'states': [
                        {
                            u'transitions': [{u'conditions': {u'min_index_age': u'1d'}, u'state_name': u'delete'}],
                            u'name': u'hot',
                            u'actions': [{u'retry': {u'count': 3, u'delay': u'1m', u'backoff': u'exponential'}, u'open': {}}]
                        },
                        {
                            u'transitions': [],
                            u'name': u'delete',
                            u'actions': [{u'retry': {u'count': 3, u'delay': u'1m', u'backoff': u'exponential'}, u'delete': {}}]
                        }
                    ],
                    u'ism_template': [{u'index_patterns': [u'what-*'], u'priority': 100, u'last_updated_time': 1668074528411}],
                    u'schema_version': 15,
                    u'policy_id': u'what'  # 目标policy_id
                },
                u'_id': u'what',
                u'_seq_no': 150078,
                u'_primary_term': 1
            }
        ]
    },
    u'content_type': u'application/json; charset=UTF-8',
    u'msg': u'OK (1919 bytes)',
    u'redirected': False,
    u'cookies_string': u''
}

解决方案

原来的判断逻辑错误,因为'what' in index_list是检查字符串是否在index_list的顶层键中,而实际policy_id嵌套在index_list.json.policies下的每个元素的policy.policy_id字段里。可以用以下两种方法实现正确判断:

方法1:使用selectattr过滤列表

通过selectattr筛选出policy.policy_id等于what的策略,再判断结果列表长度是否大于0:

- debug:
    msg: hello
  when: index_list.json.policies | selectattr('policy.policy_id', 'equalto', 'what') | list | length > 0

方法2:提取所有policy_id到列表后判断

先将所有策略的policy_id提取为一个列表,再检查what是否在列表中:

- debug:
    msg: hello
  when: 'what' in (index_list.json.policies | map(attribute='policy.policy_id') | list)

内容的提问来源于stack exchange,提问作者dzbeda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 06:21:00