Reputation: 186
I know I can print the std out using the debug
module of Ansible like below:
---
- hosts: all
tasks:
- name: list files under /root folder
command: ls /root
register: out
- name: stdout
debug: var=out.stdout_lines
And from the answer of How to use Ansible 2.0 Python API to run a Playbook?, I can run Ansible playbook with python code.
So the question is how can I get the content of the variable out
in this playbook using Ansible python api?
Upvotes: 1
Views: 4922
Reputation: 153
here is my answers, you should make a callback
# -*- coding: utf-8 -*-
import json
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.plugins import callback_loader
from ansible.plugins.callback import CallbackBase
import os
import logging
loader = DataLoader()
variable_manager = VariableManager()
inventory = Inventory(loader=loader, variable_manager=variable_manager)
variable_manager.set_inventory(inventory)
#get result output
class ResultsCollector(CallbackBase):
def __init__(self, *args, **kwargs):
super(ResultsCollector, self).__init__(*args, **kwargs)
self.host_ok = []
self.host_unreachable = []
self.host_failed = []
def v2_runner_on_unreachable(self, result, ignore_errors=False):
name = result._host.get_name()
task = result._task.get_name()
ansible_log(result)
#self.host_unreachable[result._host.get_name()] = result
self.host_unreachable.append(dict(ip=name, task=task, result=result))
def v2_runner_on_ok(self, result, *args, **kwargs):
name = result._host.get_name()
task = result._task.get_name()
if task == "setup":
pass
elif "Info" in task:
self.host_ok.append(dict(ip=name, task=task, result=result))
else:
ansible_log(result)
self.host_ok.append(dict(ip=name, task=task, result=result))
def v2_runner_on_failed(self, result, *args, **kwargs):
name = result._host.get_name()
task = result._task.get_name()
ansible_log(result)
self.host_failed.append(dict(ip=name, task=task, result=result))
class Options(object):
def __init__(self):
self.connection = "smart"
self.forks = 10
self.check = False
self.become = None
self.become_method = None
self.become_user=None
def __getattr__(self, name):
return None
options = Options()
def run_adhoc(ip,order):
variable_manager.extra_vars={"ansible_ssh_user":"root" , "ansible_ssh_pass":"passwd"}
play_source = {"name":"Ansible Ad-Hoc","hosts":"%s"%ip,"gather_facts":"no","tasks":[{"action":{"module":"command","args":"%s"%order}}]}
# play_source = {"name":"Ansible Ad-Hoc","hosts":"192.168.2.160","gather_facts":"no","tasks":[{"action":{"module":"command","args":"python ~/store.py del"}}]}
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
tqm = None
callback = ResultsCollector()
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=None,
run_tree=False,
)
tqm._stdout_callback = callback
result = tqm.run(play)
return callback
finally:
if tqm is not None:
tqm.cleanup()
def run_playbook(books):
results_callback = callback_loader.get('json')
playbooks = [books]
variable_manager.extra_vars={"ansible_ssh_user":"root" , "ansible_ssh_pass":"passwd"}
callback = ResultsCollector()
pd = PlaybookExecutor(
playbooks=playbooks,
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=None,
)
pd._tqm._stdout_callback = callback
try:
result = pd.run()
return callback
except Exception as e:
print e
if __name__ == '__main__':
#run_playbook("yml/info/process.yml")
#run_adhoc("192.168.2.149", "ifconfig")
Upvotes: 6