Merge pull request #3 from Mirantis/local_ansible

Consume ansible-playbooks without templating
This commit is contained in:
CGenie 2015-07-17 14:55:44 +02:00
commit fa734fef7a
12 changed files with 124 additions and 13 deletions

View File

@ -0,0 +1,9 @@
- hosts: localhost
sudo: yes
vars:
var1: 'playbook'
roles:
- { role: "test_role" }
tasks:
- debug: msg="VAR1 value is {{var1}}"
- fail: msg='just test failure'

View File

@ -0,0 +1,4 @@
var1: initial
uuid: stuff
def1: the_same

View File

@ -0,0 +1 @@
- debug: msg="Variable1 {{ var1 }} with uuid {{ uuid }} and default var {{ def1 }}"

View File

@ -0,0 +1,11 @@
id: ansible_sample
handler: ansible_playbook
version: 0.0.1
input:
var1:
type: str!
value: meta
uuid:
type: str!
value: 'aa1das1231'

View File

@ -0,0 +1,6 @@
- hosts: '*'
sudo: yes
vars:
default1: playbook
tasks:
- debug: msg="my message {{default1}}"

View File

@ -0,0 +1,16 @@
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:
default1:
type: str!
value: meta

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,12 +1,13 @@
# -*- 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
HANDLERS = {'ansible': Ansible,
'puppet': Puppet,
HANDLERS = {'ansible': AnsibleTemplate,
'ansible_playbook': AnsiblePlaybook,
'shell': Shell,
'none': Empty}

View File

@ -0,0 +1,51 @@
# -*- 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
from solar import errors
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 = variables['ip']
transport = C.DEFAULT_TRANSPORT
else:
host = 'localhost'
transport = 'local'
play = PlayBook(
playbook=action_file,
remote_user=remote_user,
host_list = [host],
private_key_file=private_key_file,
extra_vars=variables,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
stats=stats,
transport=transport)
play.run()
summary = stats.summarize(host)
if summary.get('unreachable') or summary.get('failures'):
raise errors.SolarError(
'Ansible playbook %s failed with next summary %s',
action_file, summary)

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,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))