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
This commit is contained in:
parent
d331a99126
commit
8ef1feca80
4
resources/ansible_sample/actions/run.yaml
Normal file
4
resources/ansible_sample/actions/run.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
- hosts: localhost
|
||||
sudo: yes
|
||||
roles:
|
||||
- { role: "test_role" }
|
@ -0,0 +1,4 @@
|
||||
|
||||
var1: initial
|
||||
uuid: stuff
|
||||
def1: the_same
|
@ -0,0 +1 @@
|
||||
- debug: msg="Variable1 {{ var1 }} with uuid {{ uuid }} and default var {{ def1 }}"
|
11
resources/ansible_sample/meta.yaml
Normal file
11
resources/ansible_sample/meta.yaml
Normal file
@ -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'
|
||||
|
@ -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)
|
||||
|
28
solar/solar/core/handlers/local_ansible/__init__.py
Normal file
28
solar/solar/core/handlers/local_ansible/__init__.py
Normal file
@ -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()
|
27
solar/solar/core/handlers/local_ansible/inventory.py
Executable file
27
solar/solar/core/handlers/local_ansible/inventory.py
Executable file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user