---
#
# Envoie votre clé dans les serveurs de l'inventaire
#
# PRE-REQUIS : une clé SSH  nommée ~/.ssh/id_ed25519.pub  (sinon, modifiez plus bas)
#
#
# https://stackoverflow.com/questions/32297456/how-to-ignore-ansible-ssh-authenticity-checking

# "SSH copie vers dalibo pour tout l inventaire"
#   (prérequis : ansible-galaxy collection install ansible.posix )"

- hosts: all
  remote_user: formateur
  become: true
  gather_facts: false
  tasks:
    #- name: Check known_hosts for {{ inventory_hostname }}
      #local_action: shell ssh-keygen -F {{ inventory_hostname }}
      #register: has_entry_in_known_hosts_file
      #changed_when: false
      #ignore_errors: true
    #- name: Ignore host key for {{ inventory_hostname }} on first run
      #when: has_entry_in_known_hosts_file.rc == 1
      #set_fact:
        #ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
    # Now that we have resolved the issue with the host key
    # we can "gather facts" without issue
    - name: Register local ssh key for formateur
      authorized_key: 
        user: formateur
        comment: "Clé formateur"
        key: "{{ lookup('file', '/home/' + lookup('env', 'USER') + '/.ssh/id_ed25519.pub') }}"
        state: present
    # à cause du ~dalibo/.bashrc pas forcément désactivé en mode interactif, copie brutale à partir de ci-dessus
    - name: Copy local key for dalibo
      become_user: root
      ansible.builtin.copy:
        remote_src: true
        src: /home/formateur/.ssh/authorized_keys
        dest: /home/dalibo/.ssh/authorized_keys
        owner: dalibo
        mode: '0644'
