Expect Module Help

54 views
Skip to first unread message

Samuel Sutton

unread,
Mar 12, 2018, 9:29:22 AM3/12/18
to Ansible Project
Hello everyone,

I'm messing around with expect and I can't seem to get a match to get the ping command to fire again a second time. I'm assuming that I'm making a very common matching mistake that is alluding me. Any help would be greatly appreciated!

~~~~~~~~~~~~~~~~~~~~~
Playbook:
~~~~~~~~~~~~~~~~~~~~~

---
- hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - name: Expect Attempt
      expect:
        echo: yes
        command: ping -c 3 127.0.0.1
        responses:
          Question:
            - rtt "ping -c 3 127.0.0.1"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Traceback:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

changed: [localhost] => {
    "changed": true,
    "cmd": "ping -c 3 127.0.0.1",
    "delta": "0:00:02.104639",
    "end": "2018-03-12 09:24:22.733808",
    "invocation": {
        "module_args": {
            "chdir": null,
            "command": "ping -c 3 127.0.0.1",
            "creates": null,
            "echo": true,
            "removes": null,
            "responses": {
                "Question": [
                    "rtt \"ping -c 3 127.0.0.1\""
                ]
            },
            "timeout": 30
        }
    },
    "rc": 0,
    "start": "2018-03-12 09:24:20.629169",
    "stdout": "PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.\r\n64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.048 ms\r\n64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.053 ms\r\n64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.063 ms\r\n\r\n--- 127.0.0.1 ping statistics ---\r\n3 packets transmitted, 3 received, 0% packet loss, time 1999ms\r\nrtt min/avg/max/mdev = 0.048/0.054/0.063/0.010 ms",
    "stdout_lines": [
        "PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.",
        "64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.048 ms",
        "64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.053 ms",
        "64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.063 ms",
        "",
        "--- 127.0.0.1 ping statistics ---",
        "3 packets transmitted, 3 received, 0% packet loss, time 1999ms",
        "rtt min/avg/max/mdev = 0.048/0.054/0.063/0.010 ms"
    ]
}
META: ran handlers
META: ran handlers

PLAY RECAP *********************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0


Kai Stian Olstad

unread,
Mar 12, 2018, 12:18:56 PM3/12/18
to ansible...@googlegroups.com
On Monday, 12 March 2018 14.29.21 CET Samuel Sutton wrote:
> I'm messing around with expect and I can't seem to get a match to get the
> ping command to fire again a second time. I'm assuming that I'm making a
> very common matching mistake that is alluding me. Any help would be greatly
> appreciated!
>
> ~~~~~~~~~~~~~~~~~~~~~
> Playbook:
> ~~~~~~~~~~~~~~~~~~~~~
>
> ---
> - hosts: localhost
> connection: local
> gather_facts: false
>
> tasks:
> - name: Expect Attempt
> expect:
> echo: yes
> command: ping -c 3 127.0.0.1
> responses:
> Question:
> - rtt "ping -c 3 127.0.0.1"

Expect is used on command and script that is interactive, ping is not interactive.

--
Kai Stian Olstad

Samuel Sutton

unread,
Mar 13, 2018, 4:05:56 PM3/13/18
to ansible...@googlegroups.com
Hey Kai,

Thanks for the reply. Is the same thing happening with expect playbook that I made to try to telnet? 

Playbook:

---
- hosts: localhost
  connection: local
  gather_facts: false

  tasks:  
    - name: Expect Attempt  
      expect:  
        echo: yes  
        command: telnet 10.233.82.7 2033  
        responses:  
          Question:  
            - Connected to port* "echo"  
            - login: "admin"  
            - Password: "password" 


File "/tmp/ansible_piMb3N/ansible_module_expect.py", line 190, in main  
    encoding='utf-8')  

fatal: [localhost]: FAILED! => {  
    "changed": true,  
    "cmd": "telnet 10.233.82.7 2033",  
    "delta": "0:00:30.122472",  
    "end": "2018-03-08 13:08:12.487220",  
    "invocation": {  
        "module_args": {  
            "chdir": null,  
            "command": "telnet 10.233.82.7 2033",  
            "creates": null,  
            "echo": true,  
            "removes": null,  
            "responses": {  
                "Question": [  
                    "Connected to port* \"echo\"",  
                    {  
                        "login": "admin"  
                    },  
                    {  
                        "Password": "password"  
                    }  
                ]  
            },  
            "timeout": 30  
        }  
    },  
    "msg": "non-zero return code",  
    "rc": 1,  
    "start": "2018-03-08 13:07:42.364748",  
    "stdout": "Trying 10.233.82.7...\r\r\nConnected to 10.233.82.7.\r\r\nEscape   character is '^]'.\r\r\nConnected to port 33. ",  
    "stdout_lines": [  
        "Trying 10.233.82.7...",  
        "",  
        "Connected to 10.233.82.7.",  
        "",  
        "Escape character is '^]'.",  
        "",  
        "Connected to port 33. "  
    ]  
}  
        to retry, use: --limit @/home/sansible/ansible/expect-r1.retry  

PLAY RECAP   *********************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1  



--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/BUJIqDOShnI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2470174.EEPk0N2yYJ%40x1.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Mar 13, 2018, 4:32:20 PM3/13/18
to ansible...@googlegroups.com
On Tuesday, 13 March 2018 21.05.41 CET Samuel Sutton wrote:
>
> Hey Kai,
>
>
> Thanks for the reply. Is the same thing happening with expect playbook that
> I made to try to telnet?

No, telnet is interactive, the problem you have now is wrong syntax.


> ---
> - hosts: localhost
> connection: local
> gather_facts: false
>
>
> tasks:
> - name: Expect Attempt
> expect:
> echo: yes
> command: telnet 10.233.82.7 2033
> responses:
> Question:
> - Connected to port* "echo"
> - login: "admin"
> - Password: "password"

Question is the text/prompt/question you are checking for, it's not a directive for the expect module.

- name: Expect Attempt
expect:
echo: yes
command: telnet 10.233.82.7 2033
responses:
login: "admin"
Password: "password"

When expect sees login in the output it will type admin and press ENTER and type password when it sees Password or password since the expect module is case insensitive by default.
I don't understand what you tried to do with 'Connected to port* "echo"'

You should also check out this tread for just a few days ago that address the same problem you are having now.
https://groups.google.com/forum/#!topic/ansible-project/Cq1NhB98vQA


--
Kai Stian Olstad

Samuel Sutton

unread,
Mar 15, 2018, 10:07:11 AM3/15/18
to ansible...@googlegroups.com
Kai,

It seems that editing out the "question:" helped.

I still have to add [Connected to port 33. :  "echo"] to my playbook though. If I omit that line, the playbook will time out.

[sansible@scspr0415857001 ansible]$ cat expect-r6.yaml

---
- hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - name: Expect Attempt
      expect:
        echo: yes
        command: telnet 10.233.82.7 2033
        responses:
          Connected to port 33. :  "echo"
          login: "admin"
          Password: "password!"




Thanks for your help!  



--
Kai Stian Olstad

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/BUJIqDOShnI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages