Skip to content

Commit

Permalink
New interface methods for role list and role argspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrews committed Nov 10, 2021
1 parent ef37547 commit 7662674
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions ansible_runner/__init__.py
Expand Up @@ -3,6 +3,7 @@
from .interface import run, run_async, \
run_command, run_command_async, \
get_plugin_docs, get_plugin_docs_async, get_plugin_list, \
get_role_list, get_role_argspec, \
get_inventory, \
get_ansible_config # noqa
from .exceptions import AnsibleRunnerException, ConfigurationError, CallbackError # noqa
Expand Down
25 changes: 25 additions & 0 deletions ansible_runner/config/doc.py
Expand Up @@ -121,3 +121,28 @@ def prepare_plugin_list_command(self, list_files=None, response_format=None, plu

self.command = [self._ansible_doc_exec_path] + self.cmdline_args
self._handle_command_wrap(self.execution_mode, self.cmdline_args)

def prepare_role_list_command(self, collection_name):
'''
ansible-doc -t role -l -j <collection_name>
'''
self._prepare_env(runner_mode=self.runner_mode)
self.cmdline_args = ['-t', 'role', '-l', '-j']
if collection_name:
self.cmdline_args.append(collection_name)

self.command = [self._ansible_doc_exec_path] + self.cmdline_args
self._handle_command_wrap(self.execution_mode, self.cmdline_args)

def prepare_role_argspec_command(self, role_name, collection_name):
'''
ansible-doc -t role -j <collection_name>.<role_name>
'''

if collection_name:
role_name = ".".join([collection_name, role_name])

self._prepare_env(runner_mode=self.runner_mode)
self.cmdline_args = ['-t', 'role', '-j', role_name]
self.command = [self._ansible_doc_exec_path] + self.cmdline_args
self._handle_command_wrap(self.execution_mode, self.cmdline_args)
52 changes: 52 additions & 0 deletions ansible_runner/interface.py
Expand Up @@ -897,3 +897,55 @@ def get_ansible_config(action, config_file=None, only_changed=None, **kwargs):
response = r.stdout.read()
error = r.stderr.read()
return response, error


def get_role_list(collection=None, **kwargs):
'''
Run an ``ansible-doc`` command to get list of installed collection roles.
'''
event_callback_handler = kwargs.pop('event_handler', None)
status_callback_handler = kwargs.pop('status_handler', None)
artifacts_handler = kwargs.pop('artifacts_handler', None)
cancel_callback = kwargs.pop('cancel_callback', None)
finished_callback = kwargs.pop('finished_callback', None)

rd = DocConfig(**kwargs)
rd.prepare_role_list_command(collection)
r = Runner(rd,
event_handler=event_callback_handler,
status_handler=status_callback_handler,
artifacts_handler=artifacts_handler,
cancel_callback=cancel_callback,
finished_callback=finished_callback)
r.run()
response = r.stdout.read()
error = r.stderr.read()
if response:
response = json.loads(santize_json_response(response))
return response, error


def get_role_argspec(role, collection=None, **kwargs):
'''
Run an ``ansible-doc`` command to get a collection role argument spec.
'''
event_callback_handler = kwargs.pop('event_handler', None)
status_callback_handler = kwargs.pop('status_handler', None)
artifacts_handler = kwargs.pop('artifacts_handler', None)
cancel_callback = kwargs.pop('cancel_callback', None)
finished_callback = kwargs.pop('finished_callback', None)

rd = DocConfig(**kwargs)
rd.prepare_role_argspec_command(role, collection)
r = Runner(rd,
event_handler=event_callback_handler,
status_handler=status_callback_handler,
artifacts_handler=artifacts_handler,
cancel_callback=cancel_callback,
finished_callback=finished_callback)
r.run()
response = r.stdout.read()
error = r.stderr.read()
if response:
response = json.loads(santize_json_response(response))
return response, error

0 comments on commit 7662674

Please sign in to comment.