Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions tests/ansible_host.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
from ansible.plugins import callback_loader
from ansible.errors import AnsibleError

def dump_ansible_results(results, stdout_callback='yaml'):
cb = callback_loader.get(stdout_callback)
return cb._dump_results(results) if cb else results

class AnsibleModuleException(AnsibleError):

"""Sub-class AnsibleError when module exceptions occur."""

def __init__(self, msg, results=None):
super(AnsibleModuleException, self).__init__(msg)
self.results = results

def __str__(self):
return "{}\nAnsible Results => {}".format(self.message, dump_ansible_results(self.results))

class ansible_host():

def __init__(self, ansible_adhoc, hostname, is_local = False):
Expand All @@ -15,8 +33,10 @@ def __getattr__(self, item):

def _run(self, *module_args, **complex_args):

module_ignore_errors = complex_args.pop('module_ignore_errors', False)

res = self.module(*module_args, **complex_args)[self.hostname]
if res.is_failed:
raise Exception("run module {} failed, errmsg {}".format(self.module_name, res))
if res.is_failed and not module_ignore_errors:
raise AnsibleModuleException("run module {} failed".format(self.module_name), res)

return res