Having issues with Expect: module

501 views
Skip to first unread message

Byron Kim

unread,
Jan 21, 2016, 6:16:00 PM1/21/16
to Ansible Project
I can't seem to find much documentation or uses of this module.  I tried reading up on pexpect documentation however, i'm unable to find my answer.

I'm having ansible run a script and then there are prompts that need to be answered.

- name: GENERATE CERTIFICATE AND KEY
  expect
:
    command
: /home/user/script arg1
    chdir
: /home/user/
    responses
:
     
(^)(?i)Enter: '{{ passphrase }}'
     
(^)(?i)Verifying: '{{ passphrase }}'
     
(^)(?i)Common: '{{ common_name }}'

When the ./script runs, it has a series of text and the prompt finishes with:

'Enter PEM pass phrase:'

is the `(^)(?i)` the correct regex I would be using to match the text 'Enter'?

Matt Martz

unread,
Jan 21, 2016, 6:23:27 PM1/21/16
to ansible...@googlegroups.com
Id recommend just starting off with using `Enter: "{{ passphrase}}"

The (?i) indicates a case insensitive search, and the parents around ^ are unnecessary, and the can be problematic as sometimes expect sees a \n or a space as the first char.
--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8d042724-c9d9-4a9c-b229-543c17ce91ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Byron Kim

unread,
Jan 21, 2016, 6:34:38 PM1/21/16
to Ansible Project
Hi Matt - thanks for the quick response.  That seemed to have done the trick.  

Thanks!


On Thursday, January 21, 2016 at 6:23:27 PM UTC-5, Matt Martz wrote:
Id recommend just starting off with using `Enter: "{{ passphrase}}"

The (?i) indicates a case insensitive search, and the parents around ^ are unnecessary, and the can be problematic as sometimes expect sees a \n or a space as the first char.

On Thursday, January 21, 2016, Byron Kim <noco...@gmail.com> wrote:
I can't seem to find much documentation or uses of this module.  I tried reading up on pexpect documentation however, i'm unable to find my answer.

I'm having ansible run a script and then there are prompts that need to be answered.

- name: GENERATE CERTIFICATE AND KEY
  expect
:
    command
: /home/user/script arg1
    chdir
: /home/user/
    responses
:
     
(^)(?i)Enter: '{{ passphrase }}'
     
(^)(?i)Verifying: '{{ passphrase }}'
     
(^)(?i)Common: '{{ common_name }}'

When the ./script runs, it has a series of text and the prompt finishes with:

'Enter PEM pass phrase:'

is the `(^)(?i)` the correct regex I would be using to match the text 'Enter'?

--
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 ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

adhithacholan karunamurthy

unread,
Jun 16, 2016, 9:01:26 AM6/16/16
to Ansible Project
Hi Matt,
          Can you please help me here on "duplicate dict key"

[kodiak@localhost 10.0.1.51]$ cat   dockerimage.yml
---
- hosts: KVMGUEST
  tasks:
     - expect:
         command:  /usr/local/sbin/install_blade.sh
         responses:
           '(?i)Enter choice:': "2"
           '(?i)Enter desired hostname:': "rhel72"
           '(?i)associate hostname with:': "oambr-ext"
           '(?i)Enter choice:': "99"
@localhost 10.0.1.51]$


    @localhost 10.0.1.51]$ ansible-playbook   --su --su-user=root --ask-su-pass  dockerimage.yml
SU password:
 [WARNING]: While constructing a mapping from /home/kodiak/10.0.1.51/dockerimage.yml, line 7, column 12, found a duplicate dict key ((?i)Enter choice:).  Using last
defined value only.


PLAY [KVMGUEST] ****************************************************************

TASK [setup] *******************************************************************
ok: [10.0.1.51]

TASK [expect] ******************************************************************
changed: [10.0.1.51]

PLAY RECAP *********************************************************************
10.0.1.51                  : ok=2    changed=1    unreachable=0    failed=0

localhost 10.0.1.51]$

Matt Martz

unread,
Jun 16, 2016, 9:08:55 AM6/16/16
to ansible...@googlegroups.com
The `expect` module has the ability to respond multiple times to a single expected string, so instead of defining the expected string 2 times, you define it once, but give it a list of responses.  Such as:

     - expect:
         command:  /usr/local/sbin/install_blade.sh
         responses:
           '(?i)Enter choice:':
             - "2"
             - "99"
           '(?i)Enter desired hostname:': "rhel72"
           '(?i)associate hostname with:': "oambr-ext"

This will require that you are running ansible 2.1, since that is the first version to have the multi answers per question functionality.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.


--
Matt Martz
@sivel
sivel.net

--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/589c82fa-246f-4616-aedf-7eb4c76702eb%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages