Run ansible job using PHP script

2,428 views
Skip to first unread message

ijunaid...@gmail.com

unread,
Feb 15, 2016, 8:14:21 AM2/15/16
to Ansible Project
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 "<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? 

Ravi Bhure

unread,
Feb 15, 2016, 1:07:52 PM2/15/16
to Ansible Project
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

Benjamin Redling

unread,
Feb 15, 2016, 4:32:08 PM2/15/16
to ansible...@googlegroups.com
On 2016-02-13 20:42, ijunaid...@gmail.com wrote:
--- %< ---
> <?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
Reply all
Reply to author
Forward
0 new messages