Commit 219855d6 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added ansible example

parent 504e1228
#!/usr/bin/env python
import json
from collections import namedtuple
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.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
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
This method could store the result in an instance attribute for retrieval later
"""
host = result._host
print json.dumps({host.name: result._result})
class Options(object):
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(check=False)
# initialize needed objects
variable_manager = ansible.vars.VariableManager()
loader = DataLoader()
# 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',
)
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
\ No newline at end of file
......@@ -19,6 +19,8 @@ __author__ = 'S. Koulouzis'
import paramiko, os
import threading
import ansible.runner
from ansible.playbook import PlayBook
def install_prerequisites(vm):
try:
......@@ -30,10 +32,7 @@ def install_prerequisites(vm):
sftp = ssh.open_sftp()
sftp.chdir('/tmp/')
stdin, stdout, stderr = ssh.exec_command("sudo sh /tmp/ansible_setup.sh")
stdout.read()
#stdin, stdout, stderr = ssh.exec_command("sudo sh /tmp/ansible_setup.sh")
stdout.read()
print "Ansible prerequisites installed in: %s " % (vm.ip)
except Exception as e:
print '%s: %s' % (vm.ip, e)
......@@ -50,5 +49,9 @@ def run(vm_list,playbook):
privatekey = vm.key
user = vm.user
if "ERROR" in ret: return ret
print ("ansible-playbook " + playbook + "-i "+ips+" --private-key="+privatekey +" --user="+user)
# 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
fdsvvvvvvvvv
\ No newline at end of file
---
- hosts: all
tasks:
- name: install sysbench
apt: name="{{ item }}" update_cache=yes state=latest
with_items:
- phoronix-test-suite
- sysbench
- expect
- git
- python-pexpect
become: true
- name: Phoronix-test-suite accept User Agreement
command: expect -c "spawn phoronix-test-suite; expect \"Do you agree to these terms and wish to proceed \(Y/n\):\"; send \"Y\n\"; expect \"Enable anonymous usage / statistics reporting \(Y/n\):\"; send \"n\n\"; expect \"Enable anonymous statistical reporting of installed software / hardware \(Y/n\):\"; send \"n\n\"; interact;"
become: true
- name: Install stream
command: phoronix-test-suite install-test stream
become: true
- name: Install iozone
command: phoronix-test-suite install-test iozone
become: true
- name: Count vCPU
command: nproc
register: vcpunumber
become: true
- debug: msg="{{ vcpunumber.stdout }}"
- name: Run sysbench
command: sysbench --test=cpu --cpu-max-prime=100000 --num-threads={{ vcpunumber.stdout }} run
register: sysbenchoutput
become: true
- debug: msg="{{ sysbenchoutput.stdout }}"
- local_action: copy content="{{ sysbenchoutput.stdout }}" dest="{{ resultslocation }}sysbench"
- name: Run stream
command: expect -c "spawn phoronix-test-suite run-test stream; expect \"Type:\"; send \"4\n\"; expect \"\(Y/n\):\"; send \"n\n\"; interact;"
#command: ls -all
register: streamoutput
become: true
- debug: msg="{{ streamoutput.stdout }}"
- local_action: copy content="{{ streamoutput.stdout }}" dest="{{ resultslocation }}stream"
- name: Run iozone
command: expect -c "spawn phoronix-test-suite run-test iozone; expect \"Record Size:\"; send \"2\n\"; expect \"File Size:\"; send \"2\n\"; expect \"Disk Test:\"; send \"3\n\"; expect \"\(Y/n\):\"; send \"n\n\"; interact;"
register: iozoneoutput
become: true
- debug: msg="{{ iozoneoutput.stdout }}"
- local_action: copy content="{{ iozoneoutput.stdout }}" dest="{{ resultslocation }}iozone"
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