I've fixed things like this before by filter through to_json.

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

On Nov 14, 2022, at 12:12 PM, Todd Lewis 
<[email protected]<mailto:[email protected]>> wrote:

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<http://elastic:9200/_opendistro/_ism/policies',%20u'changed':%20False,%20u'elapsed':%200,%20u'failed':%20False,%20u'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''}


On Monday, November 14, 2022 at 12:03:44 PM UTC-5 Todd Lewis wrote:
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''}



On Thursday, November 10, 2022 at 5:29:07 PM UTC-5 [email protected] wrote:

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




--
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 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/db2fb658-eaa1-4cfc-bc9c-b6267fec4827n%40googlegroups.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2Fdb2fb658-eaa1-4cfc-bc9c-b6267fec4827n%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C5c0900ac320744bd50be08dac6636d0a%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638040427578611414%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Kk3TA00ke7ayBoKc%2FOFgQU3C%2F2hEXO9P3sWQ4%2BdirV4%3D&reserved=0>.

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/211D07D5-D9A2-4617-8884-CA52778E1075%40nist.gov.

Reply via email to