# Run ansible job using PHP script

**URL:** <https://forum.ansible.com/t/run-ansible-job-using-php-script/20572>\
**Category:** Ansible Project\
**Created:** [February 13, 2016, 7:42pm UTC](https://forum.ansible.com/t/run-ansible-job-using-php-script/20572 "2016-02-13T19:42:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ijunaidsubhani](https://avatars.discourse-cdn.com/v4/letter/i/ea666f/32.png) [@ijunaidsubhani](https://forum.ansible.com/u/ijunaidsubhani)\
**Post date:** [February 13, 2016, 7:42pm UTC](https://forum.ansible.com/t/run-ansible-job-using-php-script/20572/1 "2016-02-13T19:42:06Z")

</div>

I am developing a web interface that will eventually be fetching information using ansible jobs.

The language I am using for the web interface is PHP.

I am trying to write a PHP script that will run an ansible playbook and eventually return the jobs results on my screen.

The question is PHP script is running everything as apache user. How can I switch over to ansible user inside my php flow.

My php script looks like:

\<?php chdir('/home/ansible/ansible'); $output = shell\_exec('pwd'); echo "
```
$output
```
"; shell\_exec('ansible-playbook si-report-api-count.yml -i production'); $output = shell\_exec('cat /tmp/log'); echo "
```
$output
```
"; ?\>

When I run this, it doesnt run the playbook. Im suspecting that the apache user cant do that. It is not even switching over to the ansible directory.

Any ideas?

---

<div class="post-metadata">

**Author:** ![Ravi\_Bhure](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@Ravi\_Bhure](https://forum.ansible.com/u/Ravi_Bhure)\
**Post date:** [February 15, 2016, 6:07pm UTC](https://forum.ansible.com/t/run-ansible-job-using-php-script/20572/2 "2016-02-15T18:07:51Z")

</div>

Sir, the most easiest way is to use cgi scripts with your php will resolve your issue, but yes you have to enable suEXEC in your Apache.

[https://httpd.apache.org/docs/2.2/suexec.html](https://httpd.apache.org/docs/2.2/suexec.html)

---

<div class="post-metadata">

**Author:** ![Benjamin\_Redling](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@Benjamin\_Redling](https://forum.ansible.com/u/Benjamin_Redling)\
**Post date:** [February 15, 2016, 9:31pm UTC](https://forum.ansible.com/t/run-ansible-job-using-php-script/20572/3 "2016-02-15T21:31:43Z")

</div>

--- %\< ---

> \<?php
> 
> chdir('/home/ansible/ansible');  
> $output = shell\_exec('pwd');  
> echo "\<pre\>$output\</pre\>";  
> shell\_exec('ansible-playbook si-report-api-count.yml -i production');
> 
> $output = shell\_exec('cat /tmp/log');  
> echo "\<pre\>$output\</pre\>";
> 
> ?\>
> 
> When I run this, it doesnt run the playbook. Im suspecting that the apache  
> user cant do that. It is not even switching over to the ansible directory.
> 
> Any ideas?

Search the net for inspiration:  
[http://stackoverflow.com/questions/5882650/how-to-execute-a-shell-script-in-php](http://stackoverflow.com/questions/5882650/how-to-execute-a-shell-script-in-php)

use sudoers, check php.ini

Regards  
Benjamin
