Unify LocalAnsible and Ansible implementations

This commit is contained in:
Dmitry Shulyak 2015-07-01 15:17:23 +03:00
parent 8ef1feca80
commit f7c7d1ad95
15 changed files with 93 additions and 76 deletions

View File

@ -0,0 +1,4 @@
- hosts: '*'
sudo: yes
roles:
- { role: "test_role" }

View File

@ -1,5 +1,5 @@
id: ansible_sample
handler: local_ansible
handler: ansible_playbook
version: 0.0.1
input:
var1:

View File

@ -0,0 +1,4 @@
- hosts: '*'
sudo: yes
tasks:
- debug: "my message"

View File

@ -0,0 +1,13 @@
id: ansible_sample
handler: ansible_playbook
version: 0.0.1
input:
ip:
type: str!
value:
ssh_user:
type: str!
value:
ssh_key:
type: str!
value:

View File

@ -1,4 +0,0 @@
- hosts: localhost
sudo: yes
roles:
- { role: "test_role" }

View File

@ -283,14 +283,14 @@ def init_cli_resource():
pass
@resource.command()
@click.argument('resource_name')
@click.argument('action_name')
def action(action_name, resource_name):
@click.argument('action')
@click.argument('resource')
def action(action, resource):
click.echo(
'action {} for resource {}'.format(action_name, resource_name)
'action {} for resource {}'.format(action, resource)
)
r = sresource.load(resource_name)
actions.resource_action(r, action_name)
actions.resource_action(sresource.load(resource), action)
@resource.command()
def compile_all():

View File

@ -1,16 +1,15 @@
# -*- coding: utf-8 -*-
from solar.core.handlers.ansible import Ansible
from solar.core.handlers.ansible_template import AnsibleTemplate
from solar.core.handlers.ansible_playbook import AnsiblePlaybook
from solar.core.handlers.base import Empty
from solar.core.handlers.puppet import Puppet
from solar.core.handlers.shell import Shell
from solar.core.handlers.local_ansible import LocalAnsible
HANDLERS = {'ansible': Ansible,
'puppet': Puppet,
HANDLERS = {'ansible': AnsibleTemplate,
'ansible_playbook': AnsiblePlaybook,
'shell': Shell,
'none': Empty,
'local_ansible': LocalAnsible}
'none': Empty}
def get(handler_name):
handler = HANDLERS.get(handler_name, None)

View File

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
import os
from ansible.playbook import PlayBook
from ansible import utils
from ansible import callbacks
import ansible.constants as C
from solar.core.handlers import base
class AnsiblePlaybook(base.BaseHandler):
def action(self, resource, action):
action_file = os.path.join(
resource.metadata['actions_path'],
resource.metadata['actions'][action])
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
variables = resource.args_dict()
remote_user = variables.get('ssh_user') or C.DEFAULT_REMOTE_USER
private_key_file = variables.get('ssh_key') or C.DEFAULT_PRIVATE_KEY_FILE
if variables.get('ip'):
host_list = [variables['ip']]
transport = C.DEFAULT_TRANSPORT
else:
host_list = ['localhost']
transport = 'local'
play = PlayBook(
playbook=action_file,
remote_user=remote_user,
host_list = host_list,
private_key_file=private_key_file,
extra_vars=variables,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
stats=stats,
transport=transport)
return play.run()

View File

@ -3,10 +3,10 @@ from fabric import api as fabric_api
import os
from solar.core.log import log
from solar.core.handlers.base import BaseHandler
from solar.core.handlers.base import TempFileHandler
class Ansible(BaseHandler):
class AnsibleTemplate(TempFileHandler):
def action(self, resource, action_name):
inventory_file = self._create_inventory(resource)
playbook_file = self._create_playbook(resource, action_name)

View File

@ -9,6 +9,18 @@ from solar.core.log import log
class BaseHandler(object):
def __init__(self, resources):
self.resources = resources
def __enter__(self):
return self
def __exit__(self, exc, value, traceback):
return
class TempFileHandler(BaseHandler):
def __init__(self, resources):
self.dst = tempfile.mkdtemp()
self.resources = resources

View File

@ -1,28 +0,0 @@
from ansible.playbook import PlayBook
from ansible import utils
from ansible import callbacks
class LocalAnsible(object):
def __init__(self, resources):
self.resources = resources
def action(self, resource, action):
action_file = os.path.join(
resource.metadata['actions_path'],
resource.metadata['actions'][action])
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
play = PlayBook(
playbook=action_file,
host_list=['localhost'],
extra_vars=resource.args_dict(),
callbacks=playbook_cb,
runner_callbacks=runner_cb,
stats=stats,
transport='local')
return play.run()

View File

@ -1,27 +0,0 @@
#!/usr/bin/python
import argparse
from ansible.playbook import PlayBook
from ansible import utils
from ansible import callbacks
def expose():
parser = argparse.ArgumentParser()
parser.add_argument('-p', type=str)
args = parser.parse_args()
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
play = PlayBook(
playbook=args.p,
host_list=['localhost'],
extra_vars={'var1': 'something', 'uuid': 'okay'},
callbacks=playbook_cb,
runner_callbacks=runner_cb,
stats=stats,
transport='local')
return play.run()
expose()

View File

@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
from fabric import api as fabric_api
from solar.core.handlers.base import BaseHandler
from solar.core.handlers.base import TempFileHandler
class Shell(BaseHandler):
class Shell(TempFileHandler):
def action(self, resource, action_name):
action_file = self._compile_action_file(resource, action_name)
fabric_api.local('bash {}'.format(action_file))