Looking for a string inside a register

19 views
Skip to first unread message

dudu.c...@gmail.com

unread,
Nov 10, 2022, 5:29:07 PM11/10/22
to Ansible Project

i’m running an API call in order to get indexes in Elastic – The output is saved inside a registry (index_list).

I want to run an additional job that runs only in case the policy name can be found in the register output – The solution I gave is not working (I’m guessing that I need to loop inside the registry on specific fields ) -

in the example below i have a policy ID named what


API Call

 - name: Get ISM_retention policy
  uri:
    url: http://elastic:9200/_opendistro/_ism/policies
    method: GET
    timeout: 180
    body_format: json
  register: index_list


The debug condition that I’m trying to run – In the register output you can find the u'policy_id': u'what'

- debug: msg: hello
  when: “'what' in index_list”

The register output

MSG:  (I have marked the policy ID in bold)

{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'}, 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''}




Todd Lewis

unread,
Nov 14, 2022, 12:03:44 PM11/14/22
to Ansible Project
In the json-ish MSG you gave us, the structure you're looking for - I think - is
MSG["json"]["policies"][*]["policy"]["policy_id"]

This "works", with a variable initialized to the string you gave us.
---
- hosts: localhost
  gather_facts: false
  tasks:
    - name: get the policies with policy_id=="what"
      debug:
        msg: '{{ item["policy"] }}'
      when: 'item["policy"]["policy_id"] == "what"'
      loop: '{{ MSG["json"]["policies"] }}'
      vars:
        MSG: {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'}, 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''}

Todd Lewis

unread,
Nov 14, 2022, 12:12:29 PM11/14/22
to Ansible Project
Um, no, it doesn't work. Because of the "u'blah'" thing.
This does work with the string you gave us, but it's the wrong fix:

---
- hosts: localhost
  gather_facts: false
  tasks:
    - name: get the policies with policy_id=="what"
      debug:
        msg: '{{ item["u''policy''"] }}'
      when: 'item["u''policy''"]["u''policy_id''"] == "u''what''"'
      loop: '{{ MSG["u''json''"]["u''policies''"] }}'

      vars:
        MSG: {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'}, 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''}

Rowe, Walter P. (Fed)

unread,
Nov 14, 2022, 12:47:02 PM11/14/22
to ansible...@googlegroups.com
I've fixed things like this before by filter through to_json.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

-- 
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/db2fb658-eaa1-4cfc-bc9c-b6267fec4827n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages