运行 使用 ansible 的 php 脚本抛出错误
running a php script using ansible throws errors
我正在尝试使用 ansible 在远程服务器上 运行 一个 php 脚本。
运行 ansible 用户脚本(ansible 用于登录服务器)完美运行。然而,当我的 php 脚本中包含 include 语句时,ansible 任务失败了。
我的 php 脚本位于
/srv/project
它试图包括
includes/someLibrary.php
当 运行 将脚本作为任何具有正确访问权限的用户使用时,一切正常,但是当 运行 通过 ansible 任务 使用它时,一切正常
- name: run script
shell: 'php /srv/project/script.php'
它失败了:
failed to open stream: No such file or directory in /srv/project/includes/someLibrary.php
运行 一个非常基本的 php 脚本虽然工作得很好。
Ansible 在非交互式 ssh 会话下运行,因此不应用用户环境设置(例如,.bashrc、.bash_profile)。这通常是 运行 交互与非交互时不同行为的原因。通过 Ansible 检查交互式 printenv 和 raw: printenv
之间的区别,您可能会发现需要设置什么(通过 ansible task/play environment:
块)才能正常工作。
我刚刚找到了问题的解决方案。
问题是,当我手动执行脚本时,我连接到服务器并在调用 php script.php
PHPs include
之前进入 /srv/project
目录,在这种情况下会查看当前目录对于我要包含的文件。当 ansible 连接到服务器时,它没有更改目录,因此产生了 no such file or directory
错误。解决这个问题的方法很简单,因为 shell 模块将 chdir
作为参数来将目录更改为 运行 命令之前指定的目录。
我的 ansible 任务现在看起来如下:
- name: run script
shell: 'php /srv/project/script.php'
args:
chdir: '/srv/project'
感谢大家的帮助!
我正在尝试使用 ansible 在远程服务器上 运行 一个 php 脚本。 运行 ansible 用户脚本(ansible 用于登录服务器)完美运行。然而,当我的 php 脚本中包含 include 语句时,ansible 任务失败了。
我的 php 脚本位于
/srv/project
它试图包括
includes/someLibrary.php当 运行 将脚本作为任何具有正确访问权限的用户使用时,一切正常,但是当 运行 通过 ansible 任务 使用它时,一切正常
- name: run script shell: 'php /srv/project/script.php'
它失败了:
failed to open stream: No such file or directory in /srv/project/includes/someLibrary.php
运行 一个非常基本的 php 脚本虽然工作得很好。
Ansible 在非交互式 ssh 会话下运行,因此不应用用户环境设置(例如,.bashrc、.bash_profile)。这通常是 运行 交互与非交互时不同行为的原因。通过 Ansible 检查交互式 printenv 和 raw: printenv
之间的区别,您可能会发现需要设置什么(通过 ansible task/play environment:
块)才能正常工作。
我刚刚找到了问题的解决方案。
问题是,当我手动执行脚本时,我连接到服务器并在调用 php script.php
PHPs include
之前进入 /srv/project
目录,在这种情况下会查看当前目录对于我要包含的文件。当 ansible 连接到服务器时,它没有更改目录,因此产生了 no such file or directory
错误。解决这个问题的方法很简单,因为 shell 模块将 chdir
作为参数来将目录更改为 运行 命令之前指定的目录。
我的 ansible 任务现在看起来如下:
- name: run script shell: 'php /srv/project/script.php' args: chdir: '/srv/project'
感谢大家的帮助!