diff --git a/README-hostgroup.md b/README-hostgroup.md
index cedca7e2729b0fcb0b00b65450bd107e47133570..e97136ca48d773b5d12ee749f22e0ef35dfae33d 100644
--- a/README-hostgroup.md
+++ b/README-hostgroup.md
@@ -150,6 +150,7 @@ Variable | Description | Required
 -------- | ----------- | --------
 `ipaadmin_principal` | The admin principal is a string and defaults to `admin` | no
 `ipaadmin_password` | The admin password is a string and is required if there is no admin ticket available on the node | no
+`ipaapi_context` | The context in which the module will execute. Executing in a server context is preferred. If not provided context will be determined by the execution environment. Valid values are `server` and `client`. | no
 `name` \| `cn` | The list of hostgroup name strings. | no
 `description` | The hostgroup description string. | no
 `nomembers` | Suppress processing of membership attributes. (bool) | no
diff --git a/tests/hostgroup/test_hostgroup.yml b/tests/hostgroup/test_hostgroup.yml
index 1b1ba7a626b7c28b36cd94b3959e1798ee19ae3f..076f76d1bf1cb027e53b7a29134760792efb0b96 100644
--- a/tests/hostgroup/test_hostgroup.yml
+++ b/tests/hostgroup/test_hostgroup.yml
@@ -1,6 +1,6 @@
 ---
 - name: Test hostgroup
-  hosts: ipaserver
+  hosts: "{{ ipa_test_host | default('ipaserver') }}"
   become: true
   gather_facts: true
 
@@ -13,6 +13,7 @@
   - name: Ensure host-group databases, mysql-server and oracle-server are absent
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name:
       - databases
       - mysql-server
@@ -22,6 +23,7 @@
   - name: Test hosts db1 and db2 absent
     ipahost:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name:
       - "{{ 'db1.' + ipaserver_domain }}"
       - "{{ 'db2.' + ipaserver_domain }}"
@@ -30,6 +32,7 @@
   - name: Host "{{ 'db1.' + ipaserver_domain }}" present
     ipahost:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: "{{ 'db1.' + ipaserver_domain }}"
       force: yes
     register: result
@@ -38,6 +41,7 @@
   - name: Host "{{ 'db2.' + ipaserver_domain }}" present
     ipahost:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: "{{ 'db2.' + ipaserver_domain }}"
       force: yes
     register: result
@@ -46,6 +50,7 @@
   - name: Ensure host-group mysql-server is present
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: mysql-server
       state: present
     register: result
@@ -54,6 +59,7 @@
   - name: Ensure host-group mysql-server is present again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: mysql-server
       state: present
     register: result
@@ -62,6 +68,7 @@
   - name: Ensure host-group oracle-server is present
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: oracle-server
       state: present
     register: result
@@ -70,6 +77,7 @@
   - name: Ensure host-group oracle-server is present again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: oracle-server
       state: present
     register: result
@@ -78,6 +86,7 @@
   - name: Ensure host-group databases is present
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       host:
@@ -90,6 +99,7 @@
   - name: Ensure host-group databases is present again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       host:
@@ -102,6 +112,7 @@
   - name: Ensure host db2 is member of host-group databases
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       host:
@@ -113,6 +124,7 @@
   - name: Ensure host db2 is member of host-group databases again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       host:
@@ -124,6 +136,7 @@
   - name: Ensure host-group mysql-server is member of host-group databases
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       hostgroup:
@@ -135,6 +148,7 @@
   - name: Ensure host-group mysql-server is member of host-group databases again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       hostgroup:
@@ -146,6 +160,7 @@
   - name: Ensure host-group oracle-server is member of host-group databases (again)
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name: databases
       state: present
       hostgroup:
@@ -157,6 +172,7 @@
   - name: Ensure host-group databases, mysql-server and oracle-server are absent
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name:
       - databases
       - mysql-server
@@ -168,6 +184,7 @@
   - name: Ensure host-group databases, mysql-server and oracle-server are absent again
     ipahostgroup:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name:
       - databases
       - mysql-server
@@ -179,6 +196,7 @@
   - name: Test hosts db1 and db2 absent
     ipahost:
       ipaadmin_password: SomeADMINpassword
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
       name:
       - "{{ 'db1.' + ipaserver_domain }}"
       - "{{ 'db2.' + ipaserver_domain }}"
diff --git a/tests/hostgroup/test_hostgroup_client_context.yml b/tests/hostgroup/test_hostgroup_client_context.yml
new file mode 100644
index 0000000000000000000000000000000000000000..a234da902f4b64dd75970a1d3bdc2c8a81a60067
--- /dev/null
+++ b/tests/hostgroup/test_hostgroup_client_context.yml
@@ -0,0 +1,37 @@
+---
+- name: Test hostgroup
+  hosts: ipaclients, ipaserver
+  become: no
+  gather_facts: no
+
+  tasks:
+  - name: Include FreeIPA facts.
+    include_tasks: ../env_freeipa_facts.yml
+
+  # Test will only be executed if host is not a server.
+  - name: Execute with server context in the client.
+    ipahostgroup:
+      ipaadmin_password: SomeADMINpassword
+      ipaapi_context: server
+      name: ThisShouldNotWork
+    register: result
+    failed_when: not (result.failed and result.msg is regex("No module named '*ipaserver'*"))
+    when: ipa_host_is_client
+
+# Import basic module tests, and execute with ipa_context set to 'client'.
+# If ipaclients is set, it will be executed using the client, if not,
+# ipaserver will be used.
+#
+# With this setup, tests can be executed against an IPA client, against
+# an IPA server using "client" context, and ensure that tests are executed
+# in upstream CI.
+
+- name: Test hostgroup using client context, in client host.
+  import_playbook: test_hostgroup.yml
+  when: groups['ipaclients']
+  vars:
+    ipa_test_host: ipaclients
+
+- name: Test hostgroup using client context, in server host.
+  import_playbook: test_hostgroup.yml
+  when: groups['ipaclients'] is not defined or not groups['ipaclients']