Unify LocalAnsible and Ansible implementations
This commit is contained in:
parent
8ef1feca80
commit
f7c7d1ad95
4
resources/ansible_local/actions/run.yaml
Normal file
4
resources/ansible_local/actions/run.yaml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
- hosts: '*'
|
||||||
|
sudo: yes
|
||||||
|
roles:
|
||||||
|
- { role: "test_role" }
|
@ -1,5 +1,5 @@
|
|||||||
id: ansible_sample
|
id: ansible_sample
|
||||||
handler: local_ansible
|
handler: ansible_playbook
|
||||||
version: 0.0.1
|
version: 0.0.1
|
||||||
input:
|
input:
|
||||||
var1:
|
var1:
|
4
resources/ansible_remote/actions/run.yaml
Normal file
4
resources/ansible_remote/actions/run.yaml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
- hosts: '*'
|
||||||
|
sudo: yes
|
||||||
|
tasks:
|
||||||
|
- debug: "my message"
|
13
resources/ansible_remote/meta.yaml
Normal file
13
resources/ansible_remote/meta.yaml
Normal 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:
|
@ -1,4 +0,0 @@
|
|||||||
- hosts: localhost
|
|
||||||
sudo: yes
|
|
||||||
roles:
|
|
||||||
- { role: "test_role" }
|
|
@ -283,14 +283,14 @@ def init_cli_resource():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
@resource.command()
|
@resource.command()
|
||||||
@click.argument('resource_name')
|
@click.argument('action')
|
||||||
@click.argument('action_name')
|
@click.argument('resource')
|
||||||
def action(action_name, resource_name):
|
def action(action, resource):
|
||||||
click.echo(
|
click.echo(
|
||||||
'action {} for resource {}'.format(action_name, resource_name)
|
'action {} for resource {}'.format(action, resource)
|
||||||
)
|
)
|
||||||
r = sresource.load(resource_name)
|
actions.resource_action(sresource.load(resource), action)
|
||||||
actions.resource_action(r, action_name)
|
|
||||||
|
|
||||||
@resource.command()
|
@resource.command()
|
||||||
def compile_all():
|
def compile_all():
|
||||||
|
@ -1,16 +1,15 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- 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.base import Empty
|
||||||
from solar.core.handlers.puppet import Puppet
|
from solar.core.handlers.puppet import Puppet
|
||||||
from solar.core.handlers.shell import Shell
|
from solar.core.handlers.shell import Shell
|
||||||
from solar.core.handlers.local_ansible import LocalAnsible
|
|
||||||
|
|
||||||
|
|
||||||
HANDLERS = {'ansible': Ansible,
|
HANDLERS = {'ansible': AnsibleTemplate,
|
||||||
'puppet': Puppet,
|
'ansible_playbook': AnsiblePlaybook,
|
||||||
'shell': Shell,
|
'shell': Shell,
|
||||||
'none': Empty,
|
'none': Empty}
|
||||||
'local_ansible': LocalAnsible}
|
|
||||||
|
|
||||||
def get(handler_name):
|
def get(handler_name):
|
||||||
handler = HANDLERS.get(handler_name, None)
|
handler = HANDLERS.get(handler_name, None)
|
||||||
|
44
solar/solar/core/handlers/ansible_playbook.py
Normal file
44
solar/solar/core/handlers/ansible_playbook.py
Normal 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()
|
@ -3,10 +3,10 @@ from fabric import api as fabric_api
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from solar.core.log import log
|
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):
|
def action(self, resource, action_name):
|
||||||
inventory_file = self._create_inventory(resource)
|
inventory_file = self._create_inventory(resource)
|
||||||
playbook_file = self._create_playbook(resource, action_name)
|
playbook_file = self._create_playbook(resource, action_name)
|
@ -9,6 +9,18 @@ from solar.core.log import log
|
|||||||
|
|
||||||
|
|
||||||
class BaseHandler(object):
|
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):
|
def __init__(self, resources):
|
||||||
self.dst = tempfile.mkdtemp()
|
self.dst = tempfile.mkdtemp()
|
||||||
self.resources = resources
|
self.resources = resources
|
||||||
|
@ -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()
|
|
@ -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()
|
|
@ -1,10 +1,10 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from fabric import api as fabric_api
|
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):
|
def action(self, resource, action_name):
|
||||||
action_file = self._compile_action_file(resource, action_name)
|
action_file = self._compile_action_file(resource, action_name)
|
||||||
fabric_api.local('bash {}'.format(action_file))
|
fabric_api.local('bash {}'.format(action_file))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user