diff --git a/resources/ansible_local/actions/run.yaml b/resources/ansible_local/actions/run.yaml new file mode 100644 index 00000000..7351a738 --- /dev/null +++ b/resources/ansible_local/actions/run.yaml @@ -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' \ No newline at end of file diff --git a/resources/ansible_local/actions/test_role/defaults/main.yml b/resources/ansible_local/actions/test_role/defaults/main.yml new file mode 100644 index 00000000..14671490 --- /dev/null +++ b/resources/ansible_local/actions/test_role/defaults/main.yml @@ -0,0 +1,4 @@ + +var1: initial +uuid: stuff +def1: the_same \ No newline at end of file diff --git a/resources/ansible_local/actions/test_role/tasks/main.yml b/resources/ansible_local/actions/test_role/tasks/main.yml new file mode 100644 index 00000000..1c628a0e --- /dev/null +++ b/resources/ansible_local/actions/test_role/tasks/main.yml @@ -0,0 +1 @@ +- debug: msg="Variable1 {{ var1 }} with uuid {{ uuid }} and default var {{ def1 }}" \ No newline at end of file diff --git a/resources/ansible_local/meta.yaml b/resources/ansible_local/meta.yaml new file mode 100644 index 00000000..cddbb391 --- /dev/null +++ b/resources/ansible_local/meta.yaml @@ -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' + diff --git a/resources/ansible_remote/actions/run.yaml b/resources/ansible_remote/actions/run.yaml new file mode 100644 index 00000000..7482f7f8 --- /dev/null +++ b/resources/ansible_remote/actions/run.yaml @@ -0,0 +1,6 @@ +- hosts: '*' + sudo: yes + vars: + default1: playbook + tasks: + - debug: msg="my message {{default1}}" \ No newline at end of file diff --git a/resources/ansible_remote/meta.yaml b/resources/ansible_remote/meta.yaml new file mode 100644 index 00000000..90026220 --- /dev/null +++ b/resources/ansible_remote/meta.yaml @@ -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 diff --git a/solar/solar/cli/main.py b/solar/solar/cli/main.py index 3073586c..49f6bd85 100644 --- a/solar/solar/cli/main.py +++ b/solar/solar/cli/main.py @@ -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(): diff --git a/solar/solar/core/handlers/__init__.py b/solar/solar/core/handlers/__init__.py index 7f854ee0..c0f5c825 100644 --- a/solar/solar/core/handlers/__init__.py +++ b/solar/solar/core/handlers/__init__.py @@ -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} diff --git a/solar/solar/core/handlers/ansible_playbook.py b/solar/solar/core/handlers/ansible_playbook.py new file mode 100644 index 00000000..06a5dd7b --- /dev/null +++ b/solar/solar/core/handlers/ansible_playbook.py @@ -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) diff --git a/solar/solar/core/handlers/ansible.py b/solar/solar/core/handlers/ansible_template.py similarity index 94% rename from solar/solar/core/handlers/ansible.py rename to solar/solar/core/handlers/ansible_template.py index a5428259..dc6e8d45 100644 --- a/solar/solar/core/handlers/ansible.py +++ b/solar/solar/core/handlers/ansible_template.py @@ -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) diff --git a/solar/solar/core/handlers/base.py b/solar/solar/core/handlers/base.py index 6da7229a..b7fe46d2 100644 --- a/solar/solar/core/handlers/base.py +++ b/solar/solar/core/handlers/base.py @@ -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 diff --git a/solar/solar/core/handlers/shell.py b/solar/solar/core/handlers/shell.py index 6bd86645..c74d4b49 100644 --- a/solar/solar/core/handlers/shell.py +++ b/solar/solar/core/handlers/shell.py @@ -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))