Skip to content

Commit b3eb41c

Browse files
Merge pull request #896 from Shrews/backport/891/release_2.1
[release_2.1] Add test and docs for running roles via API Backport of PR #891 (cherry picked from commit 7185416) Reviewed-by: Sam Doran <[email protected]> Reviewed-by: None <None>
2 parents dee646f + 09e24b8 commit b3eb41c

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

ansible_runner/interface.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def run(**kwargs):
153153
- A text INI formatted string
154154
- A list of inventory sources, or an empty list to disable passing inventory
155155
156+
:param role: Name of the role to execute.
156157
:param roles_path: Directory or list of directories to assign to ANSIBLE_ROLES_PATH
157158
:param envvars: Environment variables to be used when running Ansible. Environment variables will also be
158159
read from ``env/envvars`` in ``private_data_dir``
@@ -212,6 +213,8 @@ def run(**kwargs):
212213
:type json_mode: bool
213214
:type playbook: str or filename or list
214215
:type inventory: str or dict or list
216+
:type role: str
217+
:type roles_path: dict or list
215218
:type envvars: dict
216219
:type extravars: dict
217220
:type passwords: dict

docs/python_interface.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ Usage examples
200200
print("Final status:")
201201
print(r.stats)
202202
203+
.. code-block:: python
204+
205+
# run the role named 'myrole' contained in the '<private_data_dir>/project/roles' directory
206+
r = ansible_runner.run(private_data_dir='/tmp/demo', role='myrole')
207+
print("{}: {}".format(r.status, r.rc))
208+
print(r.stats)
209+
203210
.. code-block:: python
204211
205212
# run ansible/generic commands in interactive mode within container
@@ -219,7 +226,7 @@ Usage examples
219226
220227
.. code-block:: python
221228
222-
# run ansible/generic commands in interactive mode locally
229+
# run ansible/generic commands in interactive mode locally
223230
out, err, rc = ansible_runner.run_command(
224231
executable_cmd='ansible-playbook',
225232
cmdline_args=['gather.yaml', '-i', 'inventory', '-vvvv', '-k'],
@@ -233,7 +240,7 @@ Usage examples
233240
234241
.. code-block:: python
235242
236-
# get plugin docs from within container
243+
# get plugin docs from within container
237244
out, err = ansible_runner.get_plugin_docs(
238245
plugin_names=['vyos.vyos.vyos_command'],
239246
plugin_type='module',
@@ -246,7 +253,7 @@ Usage examples
246253
247254
.. code-block:: python
248255
249-
# get plugin docs from within container in async mode
256+
# get plugin docs from within container in async mode
250257
thread_obj, runner_obj = ansible_runner.get_plugin_docs_async(
251258
plugin_names=['ansible.netcommon.cli_config', 'ansible.netcommon.cli_command'],
252259
plugin_type='module',
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- name: "Hello World role"
2+
debug:
3+
msg: "Hello World!"

test/integration/test_interface.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,16 @@ def test_get_inventory_within_container(project_fixtures, runtime):
342342
)
343343
assert 'host_1' in out['ungrouped']['hosts']
344344
assert 'host_2' in out['ungrouped']['hosts']
345+
346+
347+
def test_run_role(project_fixtures):
348+
''' Test that we can run a role via the API. '''
349+
private_data_dir = project_fixtures / 'debug'
350+
351+
res = run(
352+
private_data_dir=private_data_dir,
353+
role='hello_world',
354+
)
355+
stdout = res.stdout.read()
356+
assert res.rc == 0, stdout
357+
assert 'Hello World!' in stdout

0 commit comments

Comments
 (0)