2

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

/srv/project

it tries to include

includes/someLibrary.php
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.

mu35li
  • 71
  • 1
  • 5
  • Does the file exist in `/srv/project/includes/someLibrary.php`? – aynber Aug 02 '16 at 16:01
  • yes it does. It also works fine when executing the script by hand. – mu35li Aug 02 '16 at 16:02
  • What are the file permissions on the file? – aynber Aug 02 '16 at 16:02
  • rwxrwx--- for www-data user and group. The ansible user is a member of the www-data group. – mu35li Aug 02 '16 at 16:04
  • You've clipped out too much details. To me it seems that file someLibrary.php is included and that there is some I/O error _inside_ that file – the error should state something like this `failed to open stream: No such file or directory in /srv/project/includes/someLibrary.php on line 31` – check that line of code. – Konstantin Suvorov Aug 02 '16 at 17:17
  • the line the error message is referring to is the one where `include` is called. My problem is that this only happens when executed via an ansible task. When I connect to the server and run the script myself it executes fine. Is there something I need to know about the ansible shell module that messes with env vars or something like that? – mu35li Aug 02 '16 at 17:57
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Aug 05 '16 at 18:59

2 Answers2

5

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'
  args:
    chdir: '/srv/project'

Thanks everyone for your help!

mu35li
  • 71
  • 1
  • 5
1

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.

nitzmahone
  • 13,720
  • 2
  • 36
  • 39
  • Thanks for the advice. I checked the envvars when logged in via ssh and when connected via ansible. Turns out they are pretty much the same with some minor differences. Nothing that would cause my problem though. – mu35li Aug 03 '16 at 08:06
  • there is a troubleshooting checklist for this kind of problems here : https://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Aug 05 '16 at 19:00