ansible 和预期问题

ansible 和预期问题

我想在 bash 脚本中使用 ansible 的 Expect 工具。首先,执行我的 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"]}

照常运行脚本没有问题。

答案1

$JAVA_HOME已为您的交互式 shell 设置,但是当您运行 Ansible playbook 时,您在远程服务器上运行该 playbook 的 shell 可能尚未设置$JAVA_HOME

不过对于 Ansible 来说这不是问题,只需在任务运行时将其设置为参数即可:

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

参考

相关内容