---
- name: Test simple kernel settings
  hosts: all
  gather_facts: true
  tags:
    - tests::reboot
  tasks:
    - name: Run test
      block:
        - name: Set platform independent vars used by this test
          include_vars:
            file: vars/vars_simple_settings.yml

        - name: Disable bootloader cmdline testing on Fedora
          set_fact:
            kernel_settings_bootloader_cmdline: null
            __kernel_settings_blcmdline_value: null
            __kernel_settings_check_reboot: false
          when: ansible_facts["distribution"] == "Fedora"

        # use public: true here so that the private role
        # variables will be exported - we use
        # __kernel_settings_profile_filename to verify
        # that the settings were applied correctly
        - name: Apply the settings - call the role
          include_role:
            name: linux-system-roles.kernel_settings
            public: true

        - name: Verify that settings were applied correctly
          include_tasks: tasks/assert_kernel_settings.yml
          vars:
            __kernel_settings_test_verify: false

        - name: Check ansible_managed, fingerprint in generated files
          include_tasks: tasks/check_header.yml
          vars:
            __file: "{{ __kernel_settings_profile_filename }}"
            __fingerprint: "system_role:kernel_settings"

        - name: Ensure role reported changed
          assert:
            that: __kernel_settings_changed | d(false)

        # reboot if a reboot is needed AND we did not tell the role
        # to reboot the machine if needed
        - name: Reboot the machine - see if settings persist after reboot
          reboot:
            test_command: tuned-adm active
          when:
            - kernel_settings_reboot_required | d(false)
            - not kernel_settings_test_reboot_ok | d(false)

        - name: Verify that settings were applied correctly after reboot
          include_tasks: tasks/assert_kernel_settings.yml
          vars:
            __kernel_settings_check_reboot: false
            __kernel_settings_test_verify: true
          when:
            - kernel_settings_reboot_required | d(false)
            - not kernel_settings_test_reboot_ok | d(false)

        - name: Apply the settings again to check idempotency
          include_role:
            name: linux-system-roles.kernel_settings

        - name: Ensure role reported not changed
          assert:
            that: not __kernel_settings_changed | d(false)

      always:
        - name: Cleanup
          tags:
            - tests::cleanup
          include_tasks: tasks/cleanup.yml
