当PHP执行shell以使用2字节字符的主机名运行Ansible时输出错误

时间:2018-05-17 08:01:00

标签: php ansible sh

我正在使用PHP在Centos 7上执行Ansible命令

以下是test.php文件中的简单代码

<?php
$command = "./run_ansible.sh";
$output = shell_exec($command);
echo "<pre>$output</pre>";
?>

和shell脚本

ANSIBLE_DIR="/etc/ansible"
ANSIBLE_INVENTORY_FILE="${ANSIBLE_DIR}/inventories/production"
COMMAND="ansible all -i ${ANSIBLE_INVENTORY_FILE} -l 北京,广州 -m ping"
sudo ${COMMAND}
  

当我在服务器上运行php文件时,它可以工作

php test.php

广州 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
北京 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

当我通过URL(http://url/test.php)在Web浏览器上运行php文件时,它失败了

?? | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
?? | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
  

主机名更改为??

关于这个案子的任何想法?

2 个答案:

答案 0 :(得分:0)

尝试使用exec:

exec("./run_ansible.sh");

并给出完整的路径:

exec("./var/www/html/run_ansible.sh");

并将文件编码设置为utf-8

答案 1 :(得分:0)

当我在shell中使用“cat,echo”命令时,PHP可以显示这些主机名。但是如果我使用“ansible”命令,输出将在浏览器上失败(显示??)

有没有人知道这个案子?