import tempfile
import logging
from mariner.util import api_endpoint, which

logger = logging.getLogger(__name__)


def make_ansible_command(stderr, stdout, hosts_file, identifier, extra_vars='{}', tag=''):
    """
    This utility will compose the command needed to run ansible, capture its
    stdout and stderr to a file
    """
    ansible_path = which('ansible-playbook')
    url = api_endpoint('tasks', identifier, 'completed')
    command = '{ansible_path} -i {hosts_file} --extra-vars="{extra_vars}" --tags="{tags}"'.format(
            ansible_path=ansible_path,
            hosts_file=hosts_file,
            extra_vars=extra_vars,
            tags=tags
    )

    redirect = '> {stdout} 2> {stderr};'.format(stdout=stdout, stderr=stderr)
    post = 'curl -X POST {url}'.format(url=url)
    return "{command} {redirect} {post}".format(command=command, redirect=redirect, post=post)


def temp_file(identifier, std):
    return tempfile.NamedTemporaryFile(
        prefix="{identifier}_{std}".format(
            identifier=identifier, std=std), delete=False
    )

@@ -68,17 +68,36 @@
 t, which
+, get_playbook_path
 %0A
-
 %0Alogger 
@@ -213,16 +213,17 @@
 %7B%7D', tag
+s
 =''):%0A  
@@ -341,24 +341,59 @@
 ile%0A    %22%22%22%0A
+    playbook = get_playbook_path()%0A
     ansible_
@@ -571,16 +571,27 @@
 %22%7Btags%7D%22
+ %7Bplaybook%7D
 '.format
@@ -722,16 +722,47 @@
 ags=tags
+,%0A            playbook=playbook
 %0A    )%0A%0A
@@ -1158,14 +1158,19 @@
 e=False%0A
-
     )
+.name
 %0A

use the new playbook_path helper