Ansible Expect Prompt set to a variable in Host not working

108 views
Skip to first unread message

phillip.from.oz

unread,
Dec 1, 2022, 4:15:15 AM12/1/22
to Ansible Project
Hi,
I'm trying to set Ansible Expect prompt to a variable in host file.

However this does not work.

You can see that Ansible is not sending a response to the prompt.
The prompt is defined as the contents of a variable declared in host file.

Can someone see what is wrong?

=======================================
This is the host file:
[trak]
BASE prompt="BASE\>"

This is the Ansible playbook:
---
- name: Test a conversation
  hosts: BASE
  remote_user: root
  gather_facts: false

  tasks:
  - name: Copy script converse.py
    copy: src=/home/converse.py
            dest=/home/converse.py
            remote_src=no
            mode=preserve
  - name: Run test session
    expect:
      echo: yes
      chdir: /tmp
      command: python3 /home/converse.py
      timeout: "300"
      responses:
        Username: sq
        Password: "MeMe"
        "{{hostvars[inventory_hostname].prompt}}": ["w ##Class(websys).test()","H"]
    register: command_output
  - debug:
      msg: "{{ command_output.stdout.split('\n') }}"




This is the result:
# ansible-playbook test-converse.yml

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

TASK [Copy script converse.py] ****************************************************************
changed: [BASE]

TASK [Run test session] ***********************************************************************
fatal: [BASE]: FAILED! =>
               {"changed": true, "cmd": "python3 converse.py", "delta": "0:05:00.340507",
                "end": "2022-12-01 19:12:18.727066", "msg": "command exceeded timeout",
                "rc": null, "start": "2022-12-01 19:07:18.386559",
                "stdout": "\r\nWelcome to BASE\r\n   My configuration is up to date\r\n
                           Print settings are normal\r\n\r\n\r\n
                Username: sq\r\n
                Password: MeMe\r\n
                BASE>",
                "stdout_lines": ["", "Welcome to BASE", "   My configuration is up to date",
                "   Print settings are normal", "", "",
                "Username: sq", "Password: MeMe", "BASE>"]}

PLAY RECAP **************************************************************************************
BASE     : ok=1    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Matt Martz

unread,
Dec 1, 2022, 9:37:26 AM12/1/22
to ansible...@googlegroups.com
Generally speaking, you cannot set keys anywhere in ansible to a template, this is simply just not allowed.

As such, there really isn't a mechanism for doing this.  Keys must be hardcoded.

--
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/e8b0cfe2-6704-45db-b87c-68da18ba4e94n%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Phillip Wu

unread,
Dec 1, 2022, 9:02:07 PM12/1/22
to ansible...@googlegroups.com
Thanks

That's a pity.

The prompt is different on each host that I'll be adding (later)

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/-rdOecvL51w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAD8N0v9ts_6KwDRWQgjW4wEknNNkwo_1WSSPFv4JdjER9_zftg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages