# "How do i fail a task in Ansible if the variable contains a boolean value?
# I want to perform input validation for Ansible playbooks"
# https://stackoverflow.com/questions/46664127/how-do-i-fail-a-task-in-ansible-if-the-variable-contains-a-boolean-value-i-want/46667499#46667499

# We assume 0-init/tasks/validate_vars.yml has DEFINITELY been run, so no need
# to re-check whether vars are defined here.  As Ansible vars cannot be unset:
# https://serverfault.com/questions/856729/how-to-destroy-delete-unset-a-variable-value-in-ansible

- name: Assert that "sugarizer_install is sameas true" (boolean not string etc)
  assert:
    that: sugarizer_install is sameas true
    fail_msg: "PLEASE SET 'sugarizer_install: True' e.g. IN: /etc/iiab/local_vars.yml"
    quiet: yes

- name: Assert that "sugarizer_enabled | type_debug == 'bool'" (boolean not string etc)
  assert:
    that: sugarizer_enabled | type_debug == 'bool'
    fail_msg: "PLEASE GIVE VARIABLE 'sugarizer_enabled' A PROPER (UNQUOTED) ANSIBLE BOOLEAN VALUE e.g. IN: /etc/iiab/local_vars.yml"
    quiet: yes


- block:

  # 3 stanzas moved up from install.yml, so Debian-or-any-OS-where-MongoDB-fails
  # still finish their "LARGE-sized" IIAB install: (WITH LOUD RED WARNINGS!)

  - name: "Set 'mongodb_install: True'"
    set_fact:
      mongodb_install: True

  - name: 'CAUTION: IF ''mongodb.service'' IS STOPPED FOR ANY REASON, IT WILL IMMEDIATELY CAUSE SUGARIZER TO FAIL ("502 Bad Gateway") !'
    debug:
      msg: "/etc/systemd/system/sugarizer.service Line 4 'Requires=mongodb.service' tries to auto-start MongoDB every time Sugarizer starts.  IIAB (roles/mongodb/tasks/enable-or-disable.yml) tries its best to keep Ansible var 'mongodb_enabled' in sync with its systemd equivalent, i.e. the output of 'systemctl is-enabled mongodb' (as of 2020-10-29 both are typically disabled, unless other apps/services/operators choose to use MongoDB)."

  - name: MONGODB - run 'mongodb' role (attempt to install MongoDB)
    include_role:
      name: mongodb


  # - name: EXIT 'sugarizer' ROLE & CONTINUE, IF 'mongodb_installed is undefined'
  #   fail:    # FORCE IT RED THIS ONCE!
  #     msg: MongoDB INSTALLATION FAILED, perhaps because MongoDB doesn't yet support Ubuntu 22.04 with libssl3?  Nevertheless IIAB will continue (consider this a warning!)
  #   when: mongodb_installed is undefined
  #   ignore_errors: yes    # RESCUE (BELOW) NOW HANDLES THIS

  - name: Verify that mongodb_installed is defined
    fail:
      msg: MongoDB INSTALLATION FAILED, perhaps because MongoDB doesn't yet support Ubuntu 22.04 with libssl3? #3190
    when: mongodb_installed is undefined

  # ELSE...

  - name: Install/Enable/Disable/Record Sugarizer (main2.yml) IF 'mongodb_installed is defined'
    include_tasks: main2.yml
    when: mongodb_installed is defined

  # THE block: APPROACH BELOW WORKS JUST LIKE main2.yml ABOVE.
  # BUT IT VISUALLY POLLUTES: MANY BLUE "skipping:" MESSAGES IN ANSIBLE'S OUTPUT.

  # - block:    # ENTIRE BLOCK CONDITIONED ON 'when: mongodb_installed is defined'
  #
  #   [MOVED TO main2.yml]
  #
  #   when: mongodb_installed is defined    # CONDITION FOR ENTIRE ABOVE block:

  rescue:

  - name: 'SEE ERROR ABOVE (skip_role_on_error: {{ skip_role_on_error }})'
    fail:
      msg: ""
    when: not skip_role_on_error
