Ansible Expect - Program under control has same prompt

59 views
Skip to first unread message

phillip.from.oz

unread,
Nov 29, 2022, 8:02:50 PM11/29/22
to Ansible Project
Hi,
I want to run a program on a remote server where after login, the program will ask for more input using the same prompt.

This is the basic flow:
Read Username
Read Password
Read commands <- these get echoed back
until 'h' or 'H' is entered

This is a sample run of the program
BASE:/home# python3 converse2.py
Welcome to BASE
   My configuration is up to date
   Print settings are normal
 Username: sq
 Password: SoLong> 
BASE>w abc
w abc
BASE>h

I run the same program using Ansible Expect with this playbook:
--- 
- name: Test a conversation
   hosts: BASE
   remote_user: root
   gather_facts: false
   tasks:
    - name: Copy script converse2.py
      copy: src=/home/converse2.py
                 dest=/home/converse2.py
                 remote_src=no
                 mode=preserve 
    - name: Run test session
      expect:
         echo: yes
         chdir: /tmp
         command: python3 /home/converse2.py
         timeout: "300"
         responses:
             Username: sq
             Password: "SoLong>"
             BASE\>: w ##Class(websys).test()
             BASE\>: H
     register: command_output
    - debug:
          msg: "{{ command_output.stdout.split('\n') }}"

However Ansible Expect is not sending 'w ##Class(websys).test()' to the program

This is the run:
ansible-playbook /home/ansible_playbooks/test-converse2.yml
[WARNING]: While constructing a mapping from /home/ansible_playbooks/test-converse2.yml, line 20, column 9, found a duplicate dict key (BASE\>). Using last defined value only.

PLAY [Test a conversation] ****************************************************************************************************************************************************

TASK [Copy script converse2.py] ***********************************************************************************************************************************************
ok: [TRAK-BASE01]

TASK [Run test session] ********************************************************************************************************************************************************
changed: [TRAK-BASE01]

TASK [debug] ********************************************************************************************************************************************************************
ok: [TRAK-BASE01] => {
    "msg": [
        "\r",
        "Welcome to BASE\r",
        "   My configuration is up to date\r",
        "   Print settings are normal\r",
        "\r",
        "\r",
        "Username: sq\r",
        "Password: SoLong>\r",
        "BASE>H"
    ]
}

PLAY RECAP ************************************************************************************************************************************************************************
TRAK-BASE01                : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

How do I fix this?

Dick Visser

unread,
Nov 30, 2022, 12:02:42 AM11/30/22
to ansible...@googlegroups.com
Your responses is now a dict which cannot have the same key more than once. So the second BASE question isn't going to work.
Try turning it into a list.


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/38895202-0733-4df4-8d11-1c661128f3f7n%40googlegroups.com.
--
Sent from Gmail Mobile

phillip.from.oz

unread,
Nov 30, 2022, 3:55:09 PM11/30/22
to Ansible Project
Thanks. That did the trick.

I changed
BASE\>: w ##Class(websys).test()
BASE\>: H
to
BASE\>: ["w ##Class(websys.PatchHistory).test()","H"]

However, how do I code the list if I have this set of prompts:
BASE1>
BASE1>
BASE1>
BASE2>
BASE2>
BASE1>
BASE1>

Dick Visser

unread,
Dec 1, 2022, 4:26:08 AM12/1/22
to ansible...@googlegroups.com
Hi

It now partly works because you made the items of a single dict key into a list and by doing so also quoted the value with '#' in it.
But what I meant is to have the responses itself be a list, so you can have duplicates. I think this would look like:

responses:
  - Username: sq
  - Password: "SoLong>"
  - BASE\>: "w ##Class(websys).test()"
  - BASE\>: "H"
  - BASE1>: "foo"
  - BASE1>: "bar"
  - BASE1>: "bz"
  - BASE2>: "fog"
  - BASE2>: "fbzhy33"
  - BASE1>: "bar"


etc



phillip.from.oz

unread,
Dec 1, 2022, 9:00:15 PM12/1/22
to Ansible Project
Thanks.

I tried what you suggested but I get:
'responses' is of type <class 'list'> and we were unable to convert to dict

But this seems to work:
     responses:
        Username: sq
        Password: "SoLong>"
        BASE\>:
          -  "w ##Class(websys).test()"
          -  "H"

Dick Visser

unread,
Dec 1, 2022, 11:28:00 PM12/1/22
to ansible...@googlegroups.com
Then i don't know, perhaps someone else does

Reply all
Reply to author
Forward
0 new messages