From 8ef1feca80c0399fd6f54e7b22a4bd83c21ce46f Mon Sep 17 00:00:00 2001 From: Dmitry Shulyak Date: Wed, 1 Jul 2015 12:13:36 +0300 Subject: [PATCH] Add local_ansible handler that will execute ansible-playbook locally This approach allows to reuse ansible roles (developer by community). Inventory is not created, all additional variables passed to playbook execution --- resources/ansible_sample/actions/run.yaml | 4 +++ .../actions/test_role/defaults/main.yml | 4 +++ .../actions/test_role/tasks/main.yml | 1 + resources/ansible_sample/meta.yaml | 11 ++++++++ solar/solar/core/handlers/__init__.py | 4 ++- .../core/handlers/local_ansible/__init__.py | 28 +++++++++++++++++++ .../core/handlers/local_ansible/inventory.py | 27 ++++++++++++++++++ 7 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 resources/ansible_sample/actions/run.yaml create mode 100644 resources/ansible_sample/actions/test_role/defaults/main.yml create mode 100644 resources/ansible_sample/actions/test_role/tasks/main.yml create mode 100644 resources/ansible_sample/meta.yaml create mode 100644 solar/solar/core/handlers/local_ansible/__init__.py create mode 100755 solar/solar/core/handlers/local_ansible/inventory.py diff --git a/resources/ansible_sample/actions/run.yaml b/resources/ansible_sample/actions/run.yaml new file mode 100644 index 00000000..fd3e8de7 --- /dev/null +++ b/resources/ansible_sample/actions/run.yaml @@ -0,0 +1,4 @@ +- hosts: localhost + sudo: yes + roles: + - { role: "test_role" } \ No newline at end of file diff --git a/resources/ansible_sample/actions/test_role/defaults/main.yml b/resources/ansible_sample/actions/test_role/defaults/main.yml new file mode 100644 index 00000000..14671490 --- /dev/null +++ b/resources/ansible_sample/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_sample/actions/test_role/tasks/main.yml b/resources/ansible_sample/actions/test_role/tasks/main.yml new file mode 100644 index 00000000..1c628a0e --- /dev/null +++ b/resources/ansible_sample/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_sample/meta.yaml b/resources/ansible_sample/meta.yaml new file mode 100644 index 00000000..fba6f9cb --- /dev/null +++ b/resources/ansible_sample/meta.yaml @@ -0,0 +1,11 @@ +id: ansible_sample +handler: local_ansible +version: 0.0.1 +input: + var1: + type: str! + value: some_value + uuid: + type: str! + value: 'aa1das1231' + diff --git a/solar/solar/core/handlers/__init__.py b/solar/solar/core/handlers/__init__.py index 7f854ee0..2dc05656 100644 --- a/solar/solar/core/handlers/__init__.py +++ b/solar/solar/core/handlers/__init__.py @@ -3,12 +3,14 @@ from solar.core.handlers.ansible import Ansible 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, 'shell': Shell, - 'none': Empty} + 'none': Empty, + 'local_ansible': LocalAnsible} def get(handler_name): handler = HANDLERS.get(handler_name, None) diff --git a/solar/solar/core/handlers/local_ansible/__init__.py b/solar/solar/core/handlers/local_ansible/__init__.py new file mode 100644 index 00000000..cb0a9a39 --- /dev/null +++ b/solar/solar/core/handlers/local_ansible/__init__.py @@ -0,0 +1,28 @@ + +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() diff --git a/solar/solar/core/handlers/local_ansible/inventory.py b/solar/solar/core/handlers/local_ansible/inventory.py new file mode 100755 index 00000000..9cec48c1 --- /dev/null +++ b/solar/solar/core/handlers/local_ansible/inventory.py @@ -0,0 +1,27 @@ +#!/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()