2016-08-02 15:58
浏览 148


I am trying to run a php script on a remote server using ansible. Running the script with the ansible user (which ansible uses to login to the server) works perfectly. The ansible task however fails when there are include statements in my php script.

My php script lays in


it tries to include

Everything works fine when running the script as any user with the correct access rights but when running it via an ansible task
- name: run script
  shell: 'php /srv/project/script.php'

it fails with:

failed to open stream: No such file or directory in /srv/project/includes/someLibrary.php

Running a very basic php script works nicely though.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用ansible在远程服务器上运行php脚本。 使用ansible用户运行脚本( ansible用于登录服务器的工作完美。 然而,当我的php脚本中包含语句时,ansible任务失败。


 / srv / project  


 includes / someLibrary.php  
 \  n 
- name:运行脚本
 shell:'php /srv/project/script.php'




  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douchenbiao0916 2016-08-03 08:03

    I just found the solution to the problem. The problem was that when I executed the script by hand, I connected to the server and cd'd into the /srv/project directory before calling php script.php PHPs include in this case looks in the current directory for the files I want to include. When ansible connects to the server it did not change the directory thus producing the no such file or directory error. The solution to this is simple as the shell module takes a chdir as an argument to change the directory to the one specified before running the command.

    My ansible task now looks as follows:

    - name: run script
      shell: 'php /srv/project/script.php'
        chdir: '/srv/project'

    Thanks everyone for your help!

    打赏 评论
  • dongxi2163 2016-08-02 20:51

    Ansible runs under a non-interactive ssh session, and thus does not apply user environment settings (eg, .bashrc, .bash_profile). This is typically the cause of different behavior when running interactively vs. not. Check the difference between an interactive printenv and raw: printenv via Ansible, and you'll probably find what needs to be set (via an ansible task/play environment: block) to get things working.

    打赏 评论

相关推荐 更多相似问题