---
# How to apply to new system:
# 1. add phonehome-port to host in datadir
# 2. ansible-playbook this
# 3. add phonehome-key to host in datadir
# 4. commit changes to datadir
# Hint: Never commit with port only. Other playbooks will fail!

- name: perfact-phonehome installation and configuration
  # skip if port is not set in datadir
  when: phonehome.port is defined
  tags: perfact-phonehome
  block:

  # create keys before install package
  - name: perfact-phonehome create dir
    become: true
    ansible.builtin.file:
      path: "/etc/perfact/phonehome/"
      state: directory
      mode: '0755'

  # add systemuser phonehome to own the keys
  - name: add systemgroup phonehome
    become: true
    ansible.builtin.group:
      name: pfphonehome
      system: true
      state: present

  - name: add systemuser phonehome to own the keys
    become: true
    ansible.builtin.user:
      name: pfphonehome
      system: true
      groups: pfphonehome
      state: present

  - name: perfact-phonehome copy phonehome private key
    become: true
    ansible.builtin.copy:
      src: "{{ inventory_hostname }}/phonehome-keys/id_rsa"
      dest: "/etc/perfact/phonehome/id_rsa"
      owner: pfphonehome
      # (might not be availible yet) group: pfphonehome
      mode: '0600'
    # if src is not present the task will fail and a new key will be build
    ignore_errors: true
    notify: start perfact-phonehome
    # no private data to logs
    diff: false

  - name: perfact-phonehome copy public key from datadir
    become: true
    ansible.builtin.copy:
      #  Hint: this structure is required because of the "-".
      #  phonehome.ssh-key is not working because it will
      #  be interpreted as phonehome.ssh.key
      content: "{{ phonehome['ssh-key'] }}\n"
      dest: "/etc/perfact/phonehome/id_rsa.pub"
      owner: pfphonehome
      # (might not be availible yet) group: pfphonehome
      mode: '0640'
    when: phonehome['ssh-key'] is defined
    notify: start perfact-phonehome

  - name: perfact-phonehome install package
    become: true
    ansible.builtin.apt:
      name: perfact-phonehome
      state: present
    # Hint: service is not enabled after installation. It will be enabled by handler.
    notify: start perfact-phonehome

  - name: perfact-phonehome chgrp of complete dir
    become: true
    ansible.builtin.file:
       path: "/etc/perfact/phonehome/"
       group: pfphonehome
       recurse: true

  - name: perfact-phonehome configure port
    become: true
    # example config - fresh installed port is always 10133
    #Host ext1.perfact.de ext2.perfact.de 87.139.192.252 37.24.183.148
    #  User pfremote
    #  RemoteForward 10133 127.0.0.1:22
    #  BatchMode yes
    #  ServerAliveInterval 20
    #  ServerAliveCountMax 3
    #  ExitOnForwardFailure yes
    #  ConnectTimeout 5
    #  UserKnownHostsFile /etc/perfact/phonehome/known_hosts
    #  IdentityFile /etc/perfact/phonehome/id_rsa
    ansible.builtin.replace:
      path: "/etc/perfact/phonehome/config"
      regexp: 'RemoteForward 10133 127.0.0.1:22'
      replace: 'RemoteForward {{ phonehome.port }} 127.0.0.1:22'
      backup: true
    notify: start perfact-phonehome


# WIP local commands are not working yet
#  - name: perfact-phonehome get filename of host in datadir
#    local_action:
#      module: command "find data/hosts/ -name {{ inventory_hostname }}"
#    register: local_hostfile
#    # also run in check-mode:
#    check_mode: false
#
#  - name: perfact-phonehome check if pubkey exits in data/hosts/...
#    local_action: command grep -e \"^phonehome:\n  ssh-key\:\s\" "{{ local_hostfile.stdout }}"
#    register: local_phonehome_present
#    ignore_errors: true
#    # also run in check-mode:
#    check_mode: false
#
  - name: perfact-phonehome get public key and add to datadir if not present
#    when: local_phonehome_present.rc != 0
    block:
    - name: perfact-phonehome get pubkey
      become: true
      ansible.builtin.command: 'cat /etc/perfact/phonehome/id_rsa.pub'
      register: phonehome_pubkey
      # also run in check-mode:
      check_mode: false

  - name: perfact-phonehome print key for datadir
    debug:
      var: phonehome_pubkey.stdout
#
#    - name: perfact-phonehome add phonehome public key to datadir if not present
#      local_action:
#        module: lineinefile
#        insertafter: "phonehome:"
#        line: "  ssh-key: {{ phonehome_pubkey.stdout }}"
#