# Looking for a string inside a register

**URL:** <https://forum.ansible.com/t/looking-for-a-string-inside-a-register/37334>\
**Category:** Ansible Project\
**Created:** [November 10, 2022, 10:29pm UTC](https://forum.ansible.com/t/looking-for-a-string-inside-a-register/37334 "2022-11-10T22:29:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [November 10, 2022, 10:29pm UTC](https://forum.ansible.com/t/looking-for-a-string-inside-a-register/37334/1 "2022-11-10T22:29:07Z")

</div>

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](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](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’'}

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 14, 2022, 5:03pm UTC](https://forum.ansible.com/t/looking-for-a-string-inside-a-register/37334/2 "2022-11-14T17:03:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 14, 2022, 5:12pm UTC](https://forum.ansible.com/t/looking-for-a-string-inside-a-register/37334/3 "2022-11-14T17:12:29Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 14, 2022, 5:46pm UTC](https://forum.ansible.com/t/looking-for-a-string-inside-a-register/37334/4 "2022-11-14T17:46:51Z")

</div>

I’ve fixed things like this before by filter through to\_json.

Walter
