#1 Prepare
- set_fact:
    common_cockpit_port: "9099"

#2 Install cockpit
- name: Install cockpit
  apt: 
    name: [cockpit*]
    update_cache: yes
  when: ansible_os_family == "Debian"

- name: Install cockpit
  yum:
    name: [cockpit*]
    update_cache: yes    
  when: ansible_os_family == "RedHat"


#3 Configure
- name: Change cockpit port 
  lineinfile:
      dest: /lib/systemd/system/cockpit.socket
      regexp: "ListenStream=9090"
      line: "ListenStream={{common_cockpit_port}}"
      backrefs: yes 

- name: Create /etc/cockpit/cockpit.conf
  file:
    path: /etc/cockpit/cockpit.conf
    owner: cockpit-ws
    group: cockpit-ws
    mode: '0640'
    state: touch

- name: Insert configuration items in cockpit.conf
  blockinfile:
    path: /etc/cockpit/cockpit.conf
    block: |
      # allow http connection, Otherwise, it redirects all HTTP connections to HTTPS
      [WebService]
      AllowUnencrypted = true

- name: Start & Enable cockpit
  shell: |
    systemctl restart cockpit
    systemctl daemon-reload
    systemctl restart cockpit.socket
    systemctl enable --now cockpit.socket
