I want to use expect tools in bash script by ansible. first, step my shell script. Checking for $JAVA_HOME variable. I set $JAVA_HOME but in Ansible, I get an error.

My env for $JAVA_HOME:

[root@portal bin]# env | grep JAVA

and my shell:

#!/usr/bin/expect -f

set timeout 1    
spawn /opt/install.sh

expect  "\[input\] Are you installing the application at the central data center? \[yes/no default: yes\]? \[yes\]\r"
send    "yes\r"

expect  "\[input\] What is the code of central data center \[default: 01\]? \[01\]\r"
send    "01\r"

expect  "What is ip or hostname of your server \[default: localhost\]? \[localhost\]\r"
send     "portal\r"

my playbook:

- name: --- run portal expect ---
  shell: |
    cd /opt/
    chmod +x portal

Finally Error in Ansible:

TASK [ansible-portal : --- run portal expect ---]
fatal: [portal]: FAILED! => {"changed": true, "cmd": "cd /opt/installer/bin/\n chmod +x portal\n ./portal", "delta": "0:00:00.015509", "end": "2018-07-07 14:01:57.971794",
"msg": "non-zero return code", "rc": 1, "start": "2018-07-07 14:01:57.956285", "stderr": "send: spawn id exp6 not open\n    
while executing\n\"send    \"yes\\r\"\"\n    (file \"./portal\" line 8)", "stderr_lines": ["send: spawn id exp6 not open", "    
while executing", "\"send    \"yes\\r\"\"", "    (file \"./portal\" line 8)"], "stdout": "spawn /opt/installer/bin/install.sh\r\nChecking input tasks\r\nSetting 
ANT_HOME\r\nChecking Java\r\nError: No JAVA_HOME is set", "stdout_lines": 
["spawn /opt/installer/bin/install.sh", "Checking input tasks", "Setting ANT_HOME",
 "Checking Java", "Error: No JAVA_HOME is set"]}

There is no problem when running script as usual.

  • I'm not very experienced with setting up Ansible, but have you tried explicitly setting environment variables in your ansible file? stackoverflow.com/questions/27733511/… – Nick ODell Jul 7 '18 at 6:07
  • java variable is true and i sure not problem. Because there is no problem when running script as usual. – pyramid13 Jul 7 '18 at 6:10

$JAVA_HOME is set for your interactive shell, but when you're running your Ansible playbook, the shell you're running that in on the remote server, likely doesn't have $JAVA_HOME set.

Not a problem for Ansible though, simply set it as a argument when the task runs:

- name: --- run portal expect ---
  shell: |
    cd /opt/
    chmod +x portal
    JAVA_HOME: /usr/java/jdk1.8.0_161


|improve this answer|||||
  • thanks @slm. this error solved. but I have another problem. in portal script i use interact and i want after the end of the questions, continue the script. But it stops. – pyramid13 Jul 7 '18 at 6:53
  • @pyramid13 - if you have follow up questions it's best to ask them individually on the site. Mark this one as accepted and ask another question. – slm Jul 7 '18 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.