---
- name: Ensure ansible_facts to get ansible_distribution
  setup:
    gather_subset: min

- name: Assert fail when used on platform where mssql_version is not supported
  when: >-
    (ansible_distribution in ['CentOS', 'RedHat']
    and ansible_distribution_major_version is version('7', '=')
    and mssql_version | int == 2022)
    or (((ansible_distribution in ['CentOS', 'RedHat']
    and ansible_distribution_major_version is version('9', '='))
    or (ansible_distribution in ['Fedora']))
    and mssql_version | int != 2022)
  block:
    - name: Run the role
      vars:
        mssql_password: "p@55w0rD"
        mssql_edition: Evaluation
      include_role:
        name: linux-system-roles.mssql

    - name: Unreachable task
      fail:
        msg: The above task must fail
  rescue:
    - name: Assert that the role failed with mssql_version not supported
      assert:
        that: >-
          'You must set the mssql_version variable to one of'
          in ansible_failed_result.msg

    - name: Clean up after the role invocation
      include_tasks: tasks/cleanup.yml

    # Putting end_host into a rescue block results in a failed task
    - name: End unsupported host
      meta: end_host
