def callPlaybook ( self , playbook , ansibleArgs , wait = True , tags = [ "all" ] ) : playbook = os . path . join ( self . playbooks , playbook ) # Path to playbook being executed verbosity = "-vvvvv" if logger . isEnabledFor ( logging . DEBUG ) else "-v" command = [ "ansible-playbook" , verbosity , "--tags" , "," . join ( tags ) , "--extra-vars" ] command . append ( " " . join ( [ "=" . join ( i ) for i in ansibleArgs . items ( ) ] ) ) # Arguments being passed to playbook command . append ( playbook ) logger . debug ( "Executing Ansible call `%s`" , " " . join ( command ) ) p = subprocess . Popen ( command ) if wait : p . communicate ( ) if p . returncode != 0 : # FIXME: parse error codes raise RuntimeError ( "Ansible reported an error when executing playbook %s" % playbook )
python how to execute ansible playbook by ansible api
Run a playbook .
def callPlaybook(self, playbook, ansibleArgs, wait=True, tags=["all"]):
        """
        Run a playbook.

        :param playbook: An Ansible playbook to run.
        :param ansibleArgs: Arguments to pass to the playbook.
        :param wait: Wait for the play to finish if true.
        :param tags: Control tags for the play.
        """
        playbook = os.path.join(self.playbooks, playbook)  # Path to playbook being executed
        verbosity = "-vvvvv" if logger.isEnabledFor(logging.DEBUG) else "-v"
        command = ["ansible-playbook", verbosity, "--tags", ",".join(tags), "--extra-vars"]
        command.append(" ".join(["=".join(i) for i in ansibleArgs.items()]))  # Arguments being passed to playbook
        command.append(playbook)

        logger.debug("Executing Ansible call `%s`", " ".join(command))
        p = subprocess.Popen(command)
        if wait:
            p.communicate()
            if p.returncode != 0:
                # FIXME: parse error codes
                raise RuntimeError("Ansible reported an error when executing playbook %s" % playbook)