# Match two strings in when condition from registered output

**URL:** <https://forum.ansible.com/t/match-two-strings-in-when-condition-from-registered-output/33123>\
**Category:** Ansible Project\
**Created:** [June 8, 2020, 8:55pm UTC](https://forum.ansible.com/t/match-two-strings-in-when-condition-from-registered-output/33123 "2020-06-08T20:55:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinay\_kumar](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@vinay\_kumar](https://forum.ansible.com/u/vinay_kumar)\
**Post date:** [June 8, 2020, 8:55pm UTC](https://forum.ansible.com/t/match-two-strings-in-when-condition-from-registered-output/33123/1 "2020-06-08T20:55:42Z")

</div>

1:

Hello  
Is it possible to search in when condition two strings from single line by matching the registered output.

For example:

the registered output consists of services and its status

1: httpd disabled  
2: tftp enabled

I want to search the line and use it in when condition.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 8, 2020, 8:58pm UTC](https://forum.ansible.com/t/match-two-strings-in-when-condition-from-registered-output/33123/2 "2020-06-08T20:58:27Z")

</div>

What is your use case?

Could you tell us more what you are trying to achieve?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [June 8, 2020, 9:12pm UTC](https://forum.ansible.com/t/match-two-strings-in-when-condition-from-registered-output/33123/3 "2020-06-08T21:12:49Z")

</div>

For example

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ output.stdout\_lines }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "'httpd' in item"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "'disabled' in item"

should give

&nbsp;&nbsp;ok: [localhost] =\> (item=1: httpd disabled) =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_loop\_var": "item",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"item": "1: httpd disabled"  
&nbsp;&nbsp;}  
&nbsp;&nbsp;skipping: [localhost] =\> (item=2: tftp enabled)

---

<div class="post-metadata">

**Author:** ![vinay\_kumar](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@vinay\_kumar](https://forum.ansible.com/u/vinay_kumar)\
**Post date:** [June 8, 2020, 9:52pm UTC](https://forum.ansible.com/t/match-two-strings-in-when-condition-from-registered-output/33123/4 "2020-06-08T21:52:25Z")

</div>

Hello @all  
Let me elaborate the question

I have 20 services to check and I would like to execute shell module one time and register its output and match the service name and its status from output lines.

for ex: I dont want to use grep in shell module for service name I want to get the status from registered output by matching the lines

shell: systemctl list-unit-files --all --state=disabled,enabled,unknown --no-legend --no-pager  
register: \_service\_status

I want to verify the service name and status with when condition from the \_service\_status.stdout lines

ex:  
comment: “tftp service is in enabled”

when: \_service\_name \_expected\_state in service\_status

comment: “httpd service is in disabled”

when: \_service\_name \_current\_state in service\_status

variables listed is mentioned in defaults/vars:

service\_name: tftp

expected\_state: enable

service\_name: httpd  
current: disable

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 9, 2020, 4:27am UTC](https://forum.ansible.com/t/match-two-strings-in-when-condition-from-registered-output/33123/5 "2020-06-09T04:27:41Z")

</div>

OK… and what would you DO with this information…?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [June 9, 2020, 5:51am UTC](https://forum.ansible.com/t/match-two-strings-in-when-condition-from-registered-output/33123/6 "2020-06-09T05:51:49Z")

</div>

Nothing. Why are you asking?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 9, 2020, 7:13am UTC](https://forum.ansible.com/t/match-two-strings-in-when-condition-from-registered-output/33123/7 "2020-06-09T07:13:15Z")

</div>

I meant what would the topic starter do with this list of services.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [June 9, 2020, 8:07am UTC](https://forum.ansible.com/t/match-two-strings-in-when-condition-from-registered-output/33123/8 "2020-06-09T08:07:50Z")

</div>

For example

- hosts: localhost  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;test\_units:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- unit: cups.service  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: enabled  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- unit: nfs-server.service  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: enabled  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- unit: zfs-mount.service  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: enabled  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- command: systemctl list-unit-files --all  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--state=disabled,enabled,unknown  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--no-legend --no-pager  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: status  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_units: "{{ my\_units|default({})|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({my\_unit: {'state': my\_state,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'preset': my\_preset}}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ status.stdout\_lines }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_list: "{{ item.split() }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_unit: "{{ my\_list.0 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_state: "{{ my\_list.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_preset: "{{ my\_list.2 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.unit }} is  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ (my\_units[item.unit].state == item.state)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ternary('', 'NOT') }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.state }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ test\_units }}"

gives

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "cups.service is enabled"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "nfs-server.service is NOT enabled"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "zfs-mount.service is enabled"

HTH,

&nbsp;&nbsp;-vlado
