---
- set_fact:
    ssh_host_public_key_key_only: "{{ lookup('hashi_vault_cache', 'secret=secret/robot/ssh_host/' + zoox_robot_name)['ssh_public_key'].split()[1] }}"
  when: ssh_host_public_key_key_only is not defined

- name: Create /etc/ssh/ssh_known_hosts
  file:
    path: "{{ asset_root_directory }}/etc/ssh/ssh_known_hosts"
    owner: root
    group: root
    mode: '644'
    state: touch
  become: yes

- name: Populate known hosts
  known_hosts:
    name: "{{ item.host }}"
    key: "{{ item.host }} {{ item.type }} {{ item.key }}"
    path: "{{ asset_root_directory }}/etc/ssh/ssh_known_hosts"
    state: present
  with_items:
    # third_party/ros/ros_comm/tools/roslaunch/src/roslaunch/remoteprocess.py#L109 expects each host specified explicitly
    - { host: "{{ zoox_hostname }}", type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'localhost', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'ai', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'main-ai', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'vision1', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'vision2', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'vision3', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'vision4', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'vision5', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'vision6', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'vision7', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'vision8', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'operator', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'simulator', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'logplay', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: '192.168.3.2', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: '192.168.3.41', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: '192.168.3.13?', type: "ssh-rsa", key: "{{ ssh_host_public_key_key_only }}" }
    - { host: 'git.zooxlabs.com', type: "ssh-rsa", key: "{{ git_zooxlabs_rsa }}" }
    - { host: 'git.zooxlabs.com', type: "ecdsa-sha2-nistp256", key: "{{ git_zooxlabs_ecdsa }}" }
    - { host: 'github.com', type: "ssh-rsa", key: "{{ github_rsa }}" }
  become: yes
