Inconsistent regex search results in where conditional?

57 views
Skip to first unread message

burn...@umn.edu

unread,
Nov 3, 2016, 4:49:40 PM11/3/16
to Ansible Project
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 <DB Info> @/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 <DB Info>
        - exit
      SQL> $:
        - "@/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
----------
0

Disconnected from 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

Expect module output (with when conditional commented out):
[/opt/wmspt]$ . ~/.profile
[/opt/wmspt]$ sqlplus <DB info>

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

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

Last Successful login time: Thu Nov 03 2016 15:09:25 -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

SQL> @/manh/software/distribution/ansible_db_select_test.sql

 USE_HTTPS
----------
0

Disconnected from 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
[/opt/wmspt]$ exit
exit

Kai Stian Olstad

unread,
Nov 3, 2016, 5:45:02 PM11/3/16
to ansible...@googlegroups.com
On 03. nov. 2016 21:27, burn...@umn.edu wrote:
> 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.

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.

--
Kai Stian Olstad

burn...@umn.edu

unread,
Nov 9, 2016, 4:00:26 PM11/9/16
to Ansible Project, ansible-pr...@olstad.com
Ah excellent! Thanks for the assistance!
Reply all
Reply to author
Forward
0 new messages