---
- name: Create host entry
  delegate_to: "{{ ipa_server }}"
  ipahost:
    ipaadmin_password: "{{ ipa_admin_password }}"
    name: "{{ host }}"
    force: yes
  tags:
  - config
  - krb5

- name: Create service entry
  delegate_to: "{{ ipa_server }}"
  ipaservice:
    ipaadmin_password: "{{ ipa_admin_password }}"
    name: "{{ service }}/{{ host }}"
    principal: "{{ principal_alias | default(omit) }}"
    force: yes
  register: ipa_service_defined
  ignore_errors: true
  tags:
  - config
  - krb5

- name: fail only when krbprincipalname isn't already defined
  fail:
    msg: "Failed to define ipa/service {{ service }}/{{ host }}"
  when:
    - ipa_service_defined is failed
    - "\"'krbprincipalname' already contains one or more values\" not in ipa_service_defined.msg"
  tags:
  - config
  - krb5
