From 608614110d3331e6a4d67836b4b314ee4240576a Mon Sep 17 00:00:00 2001
From: Rafael Guterres Jeffman <rjeffman@redhat.com>
Date: Fri, 29 Nov 2019 12:59:33 -0300
Subject: [PATCH] Added context configuration to api_connect.

This patch allows the connection to the ipa api to use a context different
than 'server', if needed.
---
 plugins/module_utils/ansible_freeipa_module.py | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/plugins/module_utils/ansible_freeipa_module.py b/plugins/module_utils/ansible_freeipa_module.py
index 12c2b29c..7977a8ea 100644
--- a/plugins/module_utils/ansible_freeipa_module.py
+++ b/plugins/module_utils/ansible_freeipa_module.py
@@ -113,7 +113,7 @@ def temp_kdestroy(ccache_dir, ccache_name):
         shutil.rmtree(ccache_dir, ignore_errors=True)
 
 
-def api_connect():
+def api_connect(context=None):
     """
     Create environment, initialize api and connect to ldap2
     """
@@ -121,9 +121,20 @@ def api_connect():
     env._bootstrap()
     env._finalize_core(**dict(DEFAULT_CONFIG))
 
-    api.bootstrap(context='server', debug=env.debug, log=None)
+    # available contexts are 'server', 'ansible-freeipa' and 'cli_installer'
+    if context is None:
+        context = 'server'
+
+    api.bootstrap(context=context, debug=env.debug, log=None)
     api.finalize()
-    api.Backend.ldap2.connect()
+
+    if api.env.in_server:
+        backend = api.Backend.ldap2
+    else:
+        backend = api.Backend.rpcclient
+
+    if not backend.isconnected():
+        backend.connect()
 
 
 def api_command(module, command, name, args):
-- 
GitLab