---
- name: Test hostgroup
  hosts: "{{ ipa_test_host | default('ipaserver') }}"
  become: true
  gather_facts: true

  tasks:
  - name: Get Domain from server name
    ansible.builtin.set_fact:
      ipaserver_domain: "{{ ansible_facts['fqdn'].split('.')[1:] | join('.') }}"
    when: ipaserver_domain is not defined

  - 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
      - oracle-server
      state: absent

  - name: Test hosts db1 and db2 absent
    ipahost:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name:
      - "{{ 'db1.' + ipaserver_domain }}"
      - "{{ 'db2.' + ipaserver_domain }}"
      state: absent

  - name: Host "{{ 'db1.' + ipaserver_domain }}" present
    ipahost:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ 'db1.' + ipaserver_domain }}"
      force: yes
    register: result
    failed_when: not result.changed or result.failed

  - name: Host "{{ 'db2.' + ipaserver_domain }}" present
    ipahost:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ 'db2.' + ipaserver_domain }}"
      force: yes
    register: result
    failed_when: not result.changed or result.failed

  - 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
    failed_when: not result.changed or result.failed

  - 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
    failed_when: result.changed or result.failed

  - 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
    failed_when: not result.changed or result.failed

  - 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
    failed_when: result.changed or result.failed

  - name: Ensure host-group databases is present
    ipahostgroup:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: databases
      state: present
      host:
      - "{{ 'db1.' + ipaserver_domain }}"
      hostgroup:
      - oracle-server
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure host-group databases is present again
    ipahostgroup:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: databases
      state: present
      host:
      - "{{ 'db1.' + ipaserver_domain }}"
      hostgroup:
      - oracle-server
    register: result
    failed_when: result.changed or result.failed

  - 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:
      - "{{ 'db2.' + ipaserver_domain }}"
      action: member
    register: result
    failed_when: not result.changed or result.failed

  - 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:
      - "{{ 'db2.' + ipaserver_domain }}"
      action: member
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure hosts db1 and db2 (no FQDN) are member of host-group databases again
    ipahostgroup:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: databases
      state: present
      host:
      - db1
      - db2
      action: member
    register: result
    failed_when: result.changed or result.failed

  - 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:
      - mysql-server
      action: member
    register: result
    failed_when: not result.changed or result.failed

  - 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:
      - mysql-server
      action: member
    register: result
    failed_when: result.changed or result.failed

  - 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:
      - oracle-server
      action: member
    register: result
    failed_when: result.changed or result.failed

  - 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
      - oracle-server
      state: absent
    register: result
    failed_when: not result.changed or result.failed

  - 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
      - oracle-server
      state: absent
    register: result
    failed_when: result.changed or result.failed

  - name: Test hosts db1 and db2 absent
    ipahost:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name:
      - "{{ 'db1.' + ipaserver_domain }}"
      - "{{ 'db2.' + ipaserver_domain }}"
      state: absent
