---
- name: Create temporary file for expected config
  tempfile:
    state: file
    suffix: .kernel_settings
  register: __kernel_settings_register_profile_conf_tempfile

- name: Put expected contents into temporary file
  copy:
    dest: "{{ __kernel_settings_register_profile_conf_tempfile.path }}"
    content: "{{ __kernel_settings_profile_file }}"
    mode: "0600"

- name: Ensure python command exists for tests below
  package:
    name: "{{ __kernel_settings_test_python_pkgs }}"
    state: present
    use: "{{ (__kernel_settings_is_ostree | d(false)) |
             ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Diff expected vs actual content
  shell: |
    {{ __kernel_settings_test_python_cmd }} -c 'import sys
    from configobj import ConfigObj
    c1 = ConfigObj(sys.argv[1])
    c2 = ConfigObj(sys.argv[2])
    sys.exit(not c1 == c2)' \
    {{ __kernel_settings_register_profile_conf_tempfile.path }} \
    {{ __kernel_settings_profile_filename }}
  register: __kernel_settings_register_profile_conf_result
  changed_when: false
  ignore_errors: true

- name: Verify expected content
  set_fact:
    __kernel_settings_success: false
  when: __kernel_settings_register_profile_conf_result is failed

- name: Show diff - may not reflect actual ConfigObj differences
  command: >-
    diff -U 4 {{ __kernel_settings_register_profile_conf_tempfile.path }}
    {{ __kernel_settings_profile_filename }}
  ignore_errors: true  # noqa ignore-errors
  when: __kernel_settings_register_profile_conf_result is failed
  changed_when: false

- name: Get active_profile file
  slurp:
    src: /etc/tuned/active_profile
  register: __kernel_settings_register_profile_conf_active_profile

# that is, the value must be exactly 'kernel_settings'
# or 'kernel_settings something'
- name: Check that active_profile ends with kernel_settings
  set_fact:
    __kernel_settings_success: false
  when: >-
    __kernel_settings_register_profile_conf_active_profile.content |
    b64decode is not search('(^| )kernel_settings$')

- name: Get profile_mode file
  slurp:
    src: /etc/tuned/profile_mode
  register: __kernel_settings_register_profile_conf_profile_mode

# that is, the value must be exactly 'kernel_settings' or
# 'kernel_settings something'
- name: Check that profile_mode is manual
  set_fact:
    __kernel_settings_success: false
  when: >-
    __kernel_settings_register_profile_conf_profile_mode.content |
    b64decode is not match('manual')

- name: Get the bootloader specific config file
  # noqa command-instead-of-module
  command: >-
    sed -n '/^TUNED_BOOT_CMDLINE=/{s/^TUNED_BOOT_CMDLINE="\(.*\)"$/\1/;p}'
    /etc/tuned/bootcmdline
  ignore_errors: true
  register: __kernel_settings_register_verify_bl_cmdline
  when:
    - __kernel_settings_blcmdline_value | d() is not none
    - __kernel_settings_blcmdline_value | d("") | length > 0
  changed_when: false

- name: Verify bootloader settings value
  set_fact:
    __kernel_settings_success: false
  when:
    - __kernel_settings_blcmdline_value | d() is not none
    - __kernel_settings_blcmdline_value | d("") | length > 0
    - __kernel_settings_register_verify_bl_cmdline is defined
    - __kernel_settings_register_verify_bl_cmdline.stdout is defined
    - __kernel_settings_register_verify_bl_cmdline is failed or
      __kernel_settings_register_verify_bl_cmdline.stdout !=
      __kernel_settings_blcmdline_value
