我想通过 ansible 在 bash 脚本中使用期望工具。首先,执行我的 shell 脚本。检查$JAVA_HOME
变量。我设置$JAVA_HOME
但在 Ansible 中,我得到一个错误。
我env
的$JAVA_HOME
:
[root@portal bin]# env | grep JAVA
JAVA_HOME=/usr/java/jdk1.8.0_161
和我的外壳:
#!/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"
我的剧本:
- name: --- run portal expect ---
shell: |
cd /opt/
chmod +x portal
./portal
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"]}
像往常一样运行脚本没有问题。