我正在尝试通过网页运行 ansible docker 容器,但我没有找到解决方案,只是部分解决方案。
我有index.html
<html>
<body>
<form action="adduser.php" method="get">
Server: <input type="text" value="<?php echo $name?>" name="name"><br>
<input type="submit">
</form>
</body>
</html>
添加用户.php
<?php
chdir("aatool");
echo getcwd();
$hosts = $_POST["name"];
echo "$hosts";
// $hosts = escapeshellarg($hosts);
//echo ".escapeshellarg($hosts)";
$output = shell_exec('sudo docker run -i --network=host centos7-ansible ansible-playbook --private-key=ky.pem --extra-vars admin_password=test -e hosts=".escapeshellarg($hosts)." chpass.yml');
echo "<pre>$output</pre>";
?>
网页输出:
/var/www/html/aatool PLAY RECAP *********************************************************************
Docker 容器日志:
PLAY RECAP *********************************************************************
Apache 错误日志:
[:error] [pid 40367] [client x.x.x.x:61812] PHP Notice: Undefined index: name in /var/www/html/adduser.php on line 4, referer: http://x.x.x.x/
如果我直接传递 hosts 值,比如 hosts=all,一切正常,但如果我尝试从 php 表单中获取值,它就无法工作。我尝试了所有我能找到的东西,这段代码给了我最好的结果,但仍然没有用。 我希望我能在这里找到一些帮助
谢谢。
最佳答案
您的表单是通过 GET 方法提交的,但是您的代码检查了 $_POST 变量。尝试从 $_GET 设置主机或将 FORM 更改为 POST。
关于php - 将表单数据作为变量传递给 docker 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54384360/