- name: Container addition
  block:
    - name: Check config user
      command: "ssh -o BatchMode=yes -o ConnectTimeout=8 -i {{ DIR_KEYS }}/{{ id }} config@{{ ip }} echo success"
      register: ssh_config_test
      failed_when: false
      changed_when: false
      when: (container_exists.proxmox_vms | default([]) | length) > 0

    - name: Set configuration user
      set_fact:
        ssh_user: "{{ 'config' if (ssh_config_test is defined and ssh_config_test.rc | default(1) == 0) else 'root' }}"

    - name: Add container
      add_host:
        name: container
        id: "{{ id }}"
        ansible_host: "{{ ip }}"
        ansible_user: "{{ ssh_user }}"
        ansible_ssh_private_key_file: "{{ [DIR_KEYS, id] | path_join }}"
        ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
        public_key: "{{ lookup('file', [DIR_KEYS, id ~ '.pub'] | path_join) }}"
        private_key: "{{ lookup('file', [DIR_KEYS, id] | path_join) }}"

    - name: Reset known hosts
      ansible.builtin.known_hosts:
        name: "{{ ip }}"
        state: absent