Commit ad821dc7 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Run playbook.yml

parent 219855d6
......@@ -7,93 +7,91 @@ 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.plugins.callback import CallbackBase
import ansible.executor.task_queue_manager
import ansible.inventory
import ansible.parsing.dataloader
import ansible.playbook.play
import ansible.plugins.callback
import ansible.vars
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.plugins import callback_loader
import os
import logging
import yaml
import sys
from results_collector import ResultsCollector
class ResultCallback(CallbackBase):
"""A sample callback plugin used for performing an action as results come in
If you want to collect all results into a single object for processing at
the end of the execution, look into utilizing the ``json`` callback plugin
or writing your own custom callback plugin
"""
def v2_runner_on_ok(self, result, **kwargs):
"""Print a json representation of the result
hosts='172.17.0.2,172.17.0.3,'
playbook_path = 'playbook.yml'
if not os.path.exists(playbook_path):
print '[ERROR] The playbook does not exist'
sys.exit()
user='vm_user'
ssh_key_file='id_ras'
extra_vars = {'resultslocation':'/tmp/res'}
variable_manager = VariableManager()
loader = DataLoader()
This method could store the result in an instance attribute for retrieval later
"""
host = result._host
print json.dumps({host.name: result._result})
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=hosts)
class Options(object):
Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check'])
def __init__(self, check=True):
self.connection = "smart"
self.module_path = None
self.forks = None
self.remote_user = None
self.private_key_file = None
self.ssh_common_args = None
self.ssh_extra_args = None
self.sftp_extra_args = None
self.scp_extra_args = None
self.become = None
self.become_method = None
self.become_user = None
self.verbosity = None
self.check = check
super(Options, self).__init__()
options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='smart', module_path=None, forks=None, remote_user=user, private_key_file=ssh_key_file, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method='sudo', become_user='root', verbosity=None, check=False)
options = Options(check=False)
# initialize needed objects
variable_manager = ansible.vars.VariableManager()
variable_manager.extra_vars = extra_vars
passwords = {}
loader = DataLoader()
pbex = PlaybookExecutor(playbooks=[playbook_path],
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
)
# Instantiate our ResultCallback for handling results as they come in
results_callback = ResultCallback()
# create inventory and pass to var manager
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='172.17.0.4,172.17.0.5')
variable_manager.set_inventory(inventory)
# create play with tasks
play_source = dict(
name = "Ansible Play",
hosts = 'all',
gather_facts = 'no',
tasks = [
dict(action=dict(module='shell', args='ls'), register='shell_out')
#,dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
]
)
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
# actually run it
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=None,
#stdout_callback='json',
stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin
#stdout_callback='default',
)
results_callback = ResultsCollector()
pbex._tqm._stdout_callback = results_callback
results = pbex.run()
ok = results_callback.host_ok
count=0;
for res in ok:
re = json.dumps(res['result']._result)
++count
target = open("ok"+str(count)+".json", 'w')
target.write(re)
target.close()
print re
count=0;
unreachable = results_callback.host_unreachable
for res in unreachable:
re = json.dumps(res['result']._result)
++count
target = open("unreachable"+str(count)+".json", 'w')
target.write(re)
target.close()
print re
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
\ No newline at end of file
count=0;
host_failed = results_callback.host_failed
for res in host_failed:
re = json.dumps(res['result']._result)
++count
target = open("host_failed"+str(count)+".json", 'w')
target.write(re)
target.close()
print re
\ No newline at end of file
......@@ -20,7 +20,7 @@ __author__ = 'S. Koulouzis'
import paramiko, os
import threading
import ansible.runner
from ansible.playbook import PlayBook
from results_collector import ResultsCollector
def install_prerequisites(vm):
try:
......@@ -50,8 +50,4 @@ def run(vm_list,playbook):
user = vm.user
if "ERROR" in ret: return ret
# construct the ansible runner and execute on all hosts
results = ansible.runner.Runner(pattern='*', forks=10, module_name='command', module_args='/usr/bin/uptime',).run()
return "SUCCESS"
\ No newline at end of file
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAvwasjBZG8lAvO0+FnNWuAtQjyrWJ8X3sZijjrVLWLqfgrbx3
837U78DDuFXspHF4/rUN5yZ4ggt/3HSrOzTVhLy0K7bQAUeZG0JQ9a4wkY7CwGfi
rgskQVyATONZs5fqBa5443qFSOLllmsMjoues7dl1/9dGqsb1NkxneC2HSp6ODb3
uri9/s8+W0r6dac3kIpRECWIEzGZGtOmAfWZwwkkSXIdjJ2gT+euDTACa1YN5Thy
CrJHzYRuDEDvTmjKab+K8hv9yzk2dWvKYMYu0WZOPnDf2e6xlVybl2uTeDVlXTIl
tu+Divo8frzyhAZdMLe89kaPAV2gI4GX7Im40QIDAQABAoIBAEnwdGM40DdEkDOd
4TLGhrczk7MHt6AQxKr6Ur4o3RxsAIxe8fL++b7fVyKHp7Qy88lrHTmGa4iymMgl
JI4jMi5RuE9fUH3eT8X8Ukf2J3h/0KQW0efaCit+eW7JZVQdjgiq8sHiD6sZoBIr
101pf4FCkJodjtUevAQUGZ9pdjQtK8HmBVGjs5q0YoJJukOqoWkk7In2HODPdKY/
Ff7aRI+QDrVGB++PREpKY7aLd4Eai5kvhqpEUK+HomSaXRkGAcgmWsS5r10ntC4T
NTPVijBXuJfglI9ytzbX72a7/g4Dom3WcOahpSm9A0kmvGP+x5CsOWIKRghCyzOK
5qapAcECgYEA9cEO+f+ilpN1Uke7uJbOWlC/+KJnHKs1d8TAYNjYKeTo+bCPAWEY
wxjM/kRnsSBNFgdpsZKZHbccHMTCDh1FDKP9Ydjry2OFc6/tTFehIfXnexOwxNCl
KldRH9yYqh1QcnlHBx3q9mwQKF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WVdMiW274OK+jx+vPKEBl0wt7z2Ro8BXaAjgZfsibjR alogo@hermes
......@@ -9,6 +9,7 @@
- expect
- git
- python-pexpect
- php-zip
become: true
- name: Phoronix-test-suite accept User Agreement
......
#!/usr/bin/env python
# Copyright 2017 S. Koulouzis
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ansible.plugins.callback import CallbackBase
__author__ = 'S. Koulouzis'
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()
#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:
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()
self.host_failed.append(dict(ip=name, task=task, result=result))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment