# Inconsistent regex search results in where conditional?

**URL:** <https://forum.ansible.com/t/inconsistent-regex-search-results-in-where-conditional/22955>\
**Category:** Ansible Project\
**Created:** [November 3, 2016, 8:27pm UTC](https://forum.ansible.com/t/inconsistent-regex-search-results-in-where-conditional/22955 "2016-11-03T20:27:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![burns498](https://avatars.discourse-cdn.com/v4/letter/b/b487fb/32.png) [@burns498](https://forum.ansible.com/u/burns498)\
**Post date:** [November 3, 2016, 8:27pm UTC](https://forum.ansible.com/t/inconsistent-regex-search-results-in-where-conditional/22955/1 "2016-11-03T20:27:41Z")

</div>

Hello - I am working through some seemingly inconsistent results for regex search results in where clauses.

I’m trying to take actions when stdout contains a certain regex pattern. I have one method using shell module, and now testing using a preferred expect module that I’m trying to get to work. The shell method properly logs based on the when-search conditional. The expect skips the same conditional, despite writing output for the same script. Below I have pasted the play, code, and both outputs for reference.

I’m new to Ansible, so perhaps I’m missing something obvious. I would expect that both conditionals would pass, but that doesn’t seem to be the case. Thoughts?

**Play:**

> [/opt/wmspt]$ ansible-playbook pb\_mhe.yml --tags=“db”,“db\_expect”
> 
> PLAY [MHE Config] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [target]
> 
> TASK [role\_mhe : Database SELECT Ansible Testing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> changed: [target]
> 
> TASK [role\_mhe : Log SELECT database output] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> changed: [target → localhost]
> 
> TASK [role\_mhe : Database expect module sqlplus testing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> changed: [target]
> 
> TASK [role\_mhe : Log EXPECT output] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> skipping: [target]
> 
> PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> target : ok=4 changed=3 unreachable=0 failed=0

[/opt/wmspt]$

**Code:**

> - name: Database SELECT Ansible Testing
> 
> tags: db
> 
> shell: “. ~/.profile;sqlplus @/manh/software/distribution/ansible\_db\_select\_test.sql”
> 
> register: db\_select\_output
> 
> - name: Log SELECT database output
> 
> tags: db
> 
> local\_action: copy content=“{{ db\_select\_output.stdout }}” dest={{ log\_dir }}/WM\_db\_select\_{{ inventory\_hostname }}-{{ lookup(‘pipe’, ‘date +%Y%m%d%H%M%S’) }}.log
> 
> when: db\_select\_output.stdout | search(“[-]+\n[\s]\*0\n”)
> 
> - name: Database expect module sqlplus testing
> 
> tags: db\_expect
> 
> expect:
> 
> command: “/bin/bash”
> 
> responses:
> 
> $ $:
> 
> - . ~/.profile
> 
> - sqlplus 
> 
> - exit
> 
> > $:
> 
> - “@/manh/software/distribution/ansible\_db\_select\_test.sql”
> 
> - quit
> 
> timeout: 5
> 
> echo: yes
> 
> register: expect\_output
> 
> - name: Log EXPECT output
> 
> tags: db\_expect
> 
> local\_action: copy content=“{{ expect\_output.stdout }}” dest={{ log\_dir }}/WM\_expect\_{{ inventory\_hostname }}-{{ lookup(‘pipe’, ‘date +%Y%m%d%H%M%S’) }}.log
> 
> when: expect\_output.stdout | search(“[-]+\n[\s]\*0\n”)

**Shell module output:**

SQL\*Plus: Release 12.1.0.2.0 Production on Thu Nov 3 15:09:25 2016

Copyright (c) 1982, 2014, Oracle. All rights reserved.

Last Successful login time: Thu Nov 03 2016 14:51:18 -05:00

Connected to:  
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production  
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,  
Advanced Analytics and Real Application Testing options

USE\_HTTPS

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 3, 2016, 9:44pm UTC](https://forum.ansible.com/t/inconsistent-regex-search-results-in-where-conditional/22955/2 "2016-11-03T21:44:30Z")

</div>

The expect output is not the same as you get from shell, line ending in expect output is \r\n.

The best way to check the output is using debug module right after the expect task like so

- debug: var=expect\_output

and then adjust your regex accordingly.

---

<div class="post-metadata">

**Author:** ![burns498](https://avatars.discourse-cdn.com/v4/letter/b/b487fb/32.png) [@burns498](https://forum.ansible.com/u/burns498)\
**Post date:** [November 9, 2016, 9:00pm UTC](https://forum.ansible.com/t/inconsistent-regex-search-results-in-where-conditional/22955/3 "2016-11-09T21:00:26Z")

</div>

Ah excellent! Thanks for the assistance!
