How to access the host alias in the inventory

I have the following inventory:

[all:vars]
ansible_ssh_extra_args="-o IdentitiesOnly=yes"
ansible_user=root
ansible_ssh_private_key_file=/mnt/c/Users/Recopy9779/.ssh/pubkeys/{{ inventory_hostname }}.pub

[homeserver]
home.server.internal ansible_host=192.168.1.2

My issue is: the public keys are stored in files that reference the hostnames, e.g. /mnt/c/Users/Recopy9779/.ssh/pubkeys/home.server.internal.pub for home.server.internal , but {{ inventory_hostname }} resolves to the IP (192.168.1.2).
All documentation I have read seems like {{ inventory_hostname }} should resolve to home.server.internal in this case.
What am I doing wrong here? Is there a way to access home.server.internal in a variable?

Hi Recopy9779. With your inventory file, I cannot reproduce the behavior you are describing. This is what I get in debug output:

<192.168.1.2> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.1.2> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o 'IdentityFile="/mnt/c/Users/Recopy9779/.ssh/pubkeys/home.server.internal.pub"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o IdentitiesOnly=yes -o 'ControlPath="/home/bvitnik/.ansible/cp/f1b6b591d3"' -tt 192.168.1.2 true

As you can see, value for IdentityFile SSH option is resolved properly. {{ inventory_hostname }} does resolve to a name, not to an IP address. ansible_host is holding the IP address.

Something else you are not showing here has led you to the wrong conclusion.

P.S. You need a private key for SSH authentication, not a public key :smile:

if you ever plan on using delegation, this might break, use inventory_hostname_short instead https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#term-inventory_hostname

Somehow restarting WSL fixed it and I am unable to reproduce it now :thinking:
Thanks
With regard to delegation , I have no idea what that is but I am just trying to get into the Ansible stuff :wink:

If you give SSH IdentitiesOnly and a public key, it will request that and only that key from the agent. This is super helpfull if you have one key per host, like I do and don’t want to trip MaxAuthTries