---
- name: Ensure that the rule runs with ssh
  hosts: all
  vars:
  # this is the outside address under which the ssh dump server is
  # known and ansible is supposed to be configured to be able to
  # connect to it (via inventory).
  # Can be set to localhost if the control node is being used as the
  # kdump target. This requires sshd to be running on the control
  # node, though.
  # In this case uncomment the tests::multihost_localhost tag below.
  # Setting it to inventory_hostname uses the managed node itself as
  # the kdump target. This makes the the test entirely single-host,
  # but is less realistic, as in practice a host can not dump core
  # to itself.
    kdump_test_ssh_server_outside: "{{ inventory_hostname }}"
    kdump_test_ssh_source: "{{ ansible_env['SSH_CONNECTION'].split()[0] if 'SSH_CONNECTION' in ansible_env else '127.0.0.1' }}"

  # this is the address at which the ssh dump server can be reached
  # from the managed host. Dumps will be uploaded there.
    kdump_test_ssh_server_inside: >-
      {{
      kdump_test_ssh_source if kdump_test_ssh_source in
      hostvars[kdump_test_ssh_server_outside]['ansible_all_ipv4_addresses']
      + hostvars[kdump_test_ssh_server_outside]['ansible_all_ipv6_addresses']
      else
      hostvars[kdump_test_ssh_server_outside]['ansible_default_ipv4']['address']
      }}

  # This is the outside address. Ansible will connect to it to
  # copy the ssh key.
    kdump_ssh_server: "{{ kdump_test_ssh_server_outside }}"
    kdump_ssh_user: >-
      {{ hostvars[kdump_test_ssh_server_outside]['ansible_user_id'] }}
    kdump_path: /tmp/tests_ssh
    kdump_target:
      type: ssh
    # This is the ssh dump server address visible from inside
    # the machine being configured. Dumps are to be copied
    # there.
      location: "{{ kdump_ssh_user }}@{{ kdump_test_ssh_server_inside }}"

# This test may execute some tasks on localhost and rely on
# localhost being a different host than the managed host
# (localhost is being used as a second host in multihost
# scenario). This would also mean that localhost would have to
# be capable enough (not just a container - must be running a
# sshd).
# This applies only when kdump_test_ssh_server_outside is set to
# localhost. In that case, uncomment the lines below and the
# corresponding line in tests_ssh_wrapper.yml
# tags:
#   - 'tests::multihost_localhost'

  tasks:
    - name: Gather facts from {{ kdump_test_ssh_server_outside }}
      setup:
      delegate_to: "{{ kdump_test_ssh_server_outside }}"
      delegate_facts: true

    - name: Print message that this test is skipped on EL 6
      debug:
        msg: Skipping the test on EL 6 when storing logs over ssh to localhost
      when:
        - ansible_distribution in ['CentOS','RedHat']
        - ansible_distribution_major_version == '6'
        - kdump_test_ssh_server_outside == inventory_hostname

  # The skip is required on EL 6 because mkdumprd on EL 6 does not work when
  # configuring ssh to localhost
    - name: Skip the test on EL 6 when control node == managed node
      meta: end_host
      when:
        - ansible_distribution in ['CentOS','RedHat']
        - ansible_distribution_major_version == '6'
        - kdump_test_ssh_server_outside == inventory_hostname

    - name: >-
        The role requires reboot only on specific systems. Hence running the
        role in a rescue block to catch when it fails with reboot.
      block:
        - name: Run the role. If reboot is not required - the play succeeds.
          include_role:
            name: fedora.linux_system_roles.kdump
      rescue:
        - name: If reboot is required - assert the expected fail message
          assert:
            that:
              - >-
                'Reboot is required to apply changes.' in
                ansible_failed_result.msg
              - kdump_reboot_required | bool

    - name: Cleanup kdump_path
      file:
        path: "{{ kdump_path }}"
        state: absent
      delegate_to: "{{ kdump_ssh_server }}"
