Error using expect to execute shell script

Issue

I am using an expect script to automatically answer prompts in a shell script designed to add Linux systems to active directory. I only have three prompts: location, username, password. I am using Ansible to execute it.

The issue when the script executes, it tries to install ~ 49 rpms on the vm, adcli, realmd, etc. Yum begins install the rpms but it never fully completes. Sometimes, it just stops after 17, 46, or 42 rpms. It doesn’t produce an error, but just inconsistently stops and the vm is obviously not joined to AD.

I know I can use Ansible to write the tasks and sidestep the use of the bash script. My goal was to at least temporarily use the work from another coworker and generate an Ansible play later on.

If I use the script outside of Ansible Tower, it works just fine. All of the functions in the shell script are executed and rpms are installed as expected. This is my first foray using expect so I may be doing something incorrectly. Thanks for taking a look.

  - name: Joining of system to the domain
    ansible.builtin.shell: |
      set timeout 10
      spawn sudo ./addto-AD
      match_max 100000

      expect -exact "Please Enter the Environment (i.e. Portand or Seattle): "
      send -- "Seattle\r"
      expect -exact "\r"

      expect -exact "Enter Elevated AD Username: "
      send -- "{{ elevated }}\r"
      expect -exact "\r"

      expect -exact "Enter Password for User: "
      send -- "{{ elevated_pass }}\r"
      expect -exact "\r"
      
      expect eof

Solution

expect eof is still subject to the timeout. The script is probably taking longer than 10 seconds to complete. I’d suggest set timeout -1


Obviously I don’t know what the script is doing, but maybe you don’t need expect for this: Try sending the responses to the script’s stdin

printf '%s\n' "Seattle" "{{ elevated }}" "{{ elevated_pass }}" | sudo ./addto-AD

Answered By – glenn jackman

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published