# SPDX-License-Identifier: MIT
---
- name: Ensure required packages are installed
  package:
    name: insights-client
    state: present
    use: "{{ __rhc_is_ostree | ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Call insights status
  include_tasks: check-insights-status.yml
  when:
    - rhc_state | d("present") != "reconnect"

- name: Add insights tags
  copy:
    dest: "{{ __rhc_insights_tags }}"
    content: "{{ rhc_insights.tags | to_nice_yaml(indent=2) }}"
    mode: '0644'
  register: __insights_tags_added
  when:
    - rhc_insights.tags is defined
    - rhc_insights.tags != __rhc_state_absent
    - rhc_insights.tags | d({}) | length > 0

- name: Delete insights tags
  file:
    state: absent
    path: "{{ __rhc_insights_tags }}"
  register: __insights_tags_removed
  when:
    - rhc_insights.tags | d(None) == __rhc_state_absent

- name: Configure autoupdate
  lineinfile:
    path: "{{ __rhc_insights_conf }}"
    regexp: "^auto_update"
    insertafter: "#auto_update"
    line: auto_update={{ rhc_insights.autoupdate | d(true) | bool }}

- name: Check ansible host in insights-client config
  when:
    - rhc_insights.ansible_host is defined
    - not rhc_insights.ansible_host is none
    - rhc_insights.ansible_host != ""
    - rhc_insights.ansible_host != __rhc_state_absent
    - rhc_insights.ansible_host != omit
  lineinfile:
    path: "{{ __rhc_insights_conf }}"
    regexp: "^ansible_host="
    state: present
    line: ansible_host={{ rhc_insights.ansible_host }}
  check_mode: true
  register: __insights_ansible_host_exists

- name: Configure ansible host
  when:
    - rhc_insights.ansible_host is defined
    - not rhc_insights.ansible_host is none
    - rhc_insights.ansible_host != ""
    - rhc_insights.ansible_host != omit
    - rhc_insights.ansible_host != __rhc_state_absent
    - __insights_ansible_host_exists.changed
  block:
    - name: Update ansible host in insights-client config
      lineinfile:
        path: "{{ __rhc_insights_conf }}"
        regexp: "^ansible_host="
        insertafter: "#ansible_host="
        line: ansible_host={{ rhc_insights.ansible_host }}
    - name: Update ansible host in inventory
      shell: >-
        insights-client --ansible-host={{ rhc_insights.ansible_host }} & wait
      when:
        - >-
          "This host is registered" in __rhc_insights_status.stdout
          or "Registered" in __rhc_insights_status.stdout
      changed_when: true

- name: Remove ansible host
  when:
    - rhc_insights.ansible_host is defined
    - rhc_insights.ansible_host == __rhc_state_absent
  block:
    - name: Remove ansible host from insights-client config
      lineinfile:
        path: "{{ __rhc_insights_conf }}"
        regexp: "^ansible_host="
        state: absent
    - name: Remove ansible host in inventory
      shell: insights-client --ansible-host= & wait
      register: __insights_ansible_host_removed
      when:
        - >-
          "This host is registered" in __rhc_insights_status.stdout
          or "Registered" in __rhc_insights_status.stdout
      changed_when: true

- name: Check display name in insights-client config
  when:
    - rhc_insights.display_name is defined
    - not rhc_insights.display_name is none
    - rhc_insights.display_name != ""
    - rhc_insights.display_name != omit
  lineinfile:
    path: "{{ __rhc_insights_conf }}"
    regexp: "^display_name="
    state: present
    line: display_name={{ rhc_insights.display_name }}
  check_mode: true
  register: __insights_display_name_exists

- name: Configure display name
  when:
    - rhc_insights.display_name is defined
    - not rhc_insights.display_name is none
    - rhc_insights.display_name != ""
    - rhc_insights.display_name != omit
    - __insights_display_name_exists.changed
  block:
    - name: Update display name in insights-client config
      lineinfile:
        path: "{{ __rhc_insights_conf }}"
        regexp: "^display_name="
        insertafter: "#display_name="
        line: display_name={{ rhc_insights.display_name }}
    - name: Update display name in inventory
      shell: >-
        insights-client --display-name={{ rhc_insights.display_name }} & wait
      when:
        - >-
          "This host is registered" in __rhc_insights_status.stdout
          or "Registered" in __rhc_insights_status.stdout
      changed_when: true

- name: Register insights-client
  shell: insights-client --register & wait
  when:
    - >-
      rhc_state | d("present") == "reconnect"
      or "NOT" in __rhc_insights_status.stdout
      or "unregistered" in __rhc_insights_status.stdout
  register: __rhc_insights_registration_result
  changed_when: true

- name: Do the collection if insights-client was registered
  shell: insights-client & wait
  when:
    - >-
      "This host is registered" in __rhc_insights_status.stdout
      or "Registered" in __rhc_insights_status.stdout
    - >-
      __insights_tags_added is changed
      or __insights_tags_removed is changed
  changed_when: true

- name: Configure remediation
  when:
    - ansible_distribution_version is version("8.4", ">=")
    - rhc_insights.remediation | d("present") == "present"
  block:
    - name: Ensure worker and ansible packages are installed
      package:
        name:
          - rhc
          - rhc-worker-playbook
          - ansible-core
        state: present
        use: "{{ __rhc_is_ostree | ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

    - name: Enable remediation
      systemd:
        enabled: true
        state: started
        name: "{{ __rhc_yggdrasil_systemd_service }}"

- name: Disable remediation
  systemd:
    enabled: false
    state: stopped
    name: "{{ __rhc_yggdrasil_systemd_service }}"
  when:
    - rhc_insights.remediation | d("present") == "absent"
    - '"rhc" in ansible_facts.packages'
