Run ansible job using PHP script

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?

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

--- %< ---

<?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

use sudoers, check php.ini

Regards
Benjamin