- name: Read and display local SSH public key with user and host info hosts: localhost connection: local gather_facts: true # Required to access ansible_user and ansible_hostname vars: ssh_key_type: "rsa" public_key_path: "{{ lookup('env', 'HOME') + '/.ssh/id_' + ssh_key_type + '.pub' }}" tasks: - name: Check if SSH public key file exists ansible.builtin.stat: path: "{{ public_key_path }}" register: ssh_key_file tags: [check] - name: Fail if SSH public key is missing ansible.builtin.fail: msg: "Public SSH key not found at {{ public_key_path }}." when: not ssh_key_file.stat.exists tags: [fail] - name: Read SSH public key content ansible.builtin.slurp: src: "{{ public_key_path }}" register: local_public_key when: ssh_key_file.stat.exists tags: [read] - name: Get current user's UID ansible.builtin.command: id -u register: user_uid changed_when: false tags: [info] - name: Get current user's GID ansible.builtin.command: id -g register: user_gid changed_when: false tags: [info] - name: Get Docker host (default gateway) IP ansible.builtin.shell: "ip route | awk '/default/ {print $3}'" register: docker_host_ip changed_when: false tags: [host_ip] - name: Try resolving host.docker.internal command: getent hosts host.docker.internal register: docker_dns_host failed_when: false changed_when: false tags: [host_ip] - name: Display SSH key with user and host information ansible.builtin.debug: msg: | SSH Public Key Info --------------------- User : {{ ansible_user }} User : {{ ansible_facts['user_id'] }} UID : {{ user_uid.stdout }} GID : {{ user_gid.stdout }} Host : {{ ansible_hostname }} IP Addr : {{ ansible_default_ipv4.address }} IPs : {{ ansible_all_ipv4_addresses }} Host : {{ ansible_facts['hostname'] }} Docker Host IP: {{ docker_host_ip.stdout }} Host.docker.internal resolves to: {{ docker_dns_host.stdout }} Path : {{ public_key_path }} Key: {{ local_public_key.content | b64decode }} tags: [show] - name: Set decoded SSH public key as fact set_fact: decoded_ssh_key: "{{ local_public_key.content | b64decode }}" tags: [read, set_fact]